Unix Technical Forum

RE: adding 3 values

This is a discussion on RE: adding 3 values within the MySQL General forum forums, part of the MySQL category; --> > > -----Original Message----- > > From: ross@aztechost.com [mailto:ross@aztechost.com] > > Sent: 10 May 2007 10:08 > > To: ...


Go Back   Unix Technical Forum > Database Server Software > MySQL > MySQL General forum

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 06:22 AM
Edward Kay
 
Posts: n/a
Default RE: adding 3 values

> > -----Original Message-----
> > From: ross@aztechost.com [mailto:ross@aztechost.com]
> > Sent: 10 May 2007 10:08
> > To: mysql@lists.mysql.com
> > Subject: adding 3 values
> >
> >
> > Hi,
> >
> > I have 3 integer values in the table single_rooms, double_rooms,
> > twin _ooms but want to add them all up to do a comparison to see
> > if the combined number of rooms is less than ten.
> >
> > Ta,
> >
> > R.
> >
> >

>
> SELECT SUM(single_rooms, double_rooms, twin_rooms) from TABLE;
>
> Or, if you want a boolean value depending if there are less than 10:
>
> SELECT IF(SUM(single_rooms, double_rooms, twin_rooms) < 10,1,0)
> from TABLE;
>


Sorry, brain was switched off when I wrote that. It should be:

SELECT single_rooms+double_rooms+twin_rooms from TABLE;

Or, if you want a boolean value depending if there are less than 10:

SELECT IF((single_rooms+double_rooms+twin_rooms) < 10,1,0) from TABLE;

Edward
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 06:22 AM
ross@aztechost.com
 
Posts: n/a
Default Re: adding 3 values

Ok, I have this so far

$query = "SELECT * FROM properties where
single_rooms+double_rooms+twin_rooms<10 and rent < 100";

This is fine but what I really want to do it this

$query = "SELECT * FROM properties WHERE
single_rooms+double_rooms+twin_rooms<10 AND
single_rooms+double_rooms+twin_rooms<10 AND rent < 100";

This is starting to get messy. Can I set up an alias for the total? I tried
this without success.

$query = "SELECT *, single_rooms+double_rooms+twin_rooms AS total FROM
properties WHERE total >2 AND total <10


R.

----- Original Message -----
From: "Edward Kay" <edward@labhut.com>
To: <ross@aztechost.com>; <mysql@lists.mysql.com>
Sent: Thursday, May 10, 2007 10:17 AM
Subject: RE: adding 3 values


>> > -----Original Message-----
>> > From: ross@aztechost.com [mailto:ross@aztechost.com]
>> > Sent: 10 May 2007 10:08
>> > To: mysql@lists.mysql.com
>> > Subject: adding 3 values
>> >
>> >
>> > Hi,
>> >
>> > I have 3 integer values in the table single_rooms, double_rooms,
>> > twin _ooms but want to add them all up to do a comparison to see
>> > if the combined number of rooms is less than ten.
>> >
>> > Ta,
>> >
>> > R.
>> >
>> >

>>
>> SELECT SUM(single_rooms, double_rooms, twin_rooms) from TABLE;
>>
>> Or, if you want a boolean value depending if there are less than 10:
>>
>> SELECT IF(SUM(single_rooms, double_rooms, twin_rooms) < 10,1,0)
>> from TABLE;
>>

>
> Sorry, brain was switched off when I wrote that. It should be:
>
> SELECT single_rooms+double_rooms+twin_rooms from TABLE;
>
> Or, if you want a boolean value depending if there are less than 10:
>
> SELECT IF((single_rooms+double_rooms+twin_rooms) < 10,1,0) from TABLE;
>
> Edward
>
> --
> MySQL General Mailing List
> For list archives: http://lists.mysql.com/mysql
> To unsubscribe: http://lists.mysql.com/mysql?unsub=ross@aztechost.com
>
>



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-28-2008, 06:22 AM
Peter Brawley
 
Posts: n/a
Default Re: adding 3 values

>This is starting to get messy.
>Can I set up an alias for the total?


That's exactly what HAVING is for.

PB

ross@aztechost.com wrote:
> Ok, I have this so far
>
> $query = "SELECT * FROM properties where
> single_rooms+double_rooms+twin_rooms<10 and rent < 100";
>
> This is fine but what I really want to do it this
>
> $query = "SELECT * FROM properties WHERE
> single_rooms+double_rooms+twin_rooms<10 AND
> single_rooms+double_rooms+twin_rooms<10 AND rent < 100";
>
> This is starting to get messy. Can I set up an alias for the total? I
> tried this without success.
>
> $query = "SELECT *, single_rooms+double_rooms+twin_rooms AS total
> FROM properties WHERE total >2 AND total <10
>
>
> R.
>
> ----- Original Message ----- From: "Edward Kay" <edward@labhut.com>
> To: <ross@aztechost.com>; <mysql@lists.mysql.com>
> Sent: Thursday, May 10, 2007 10:17 AM
> Subject: RE: adding 3 values
>
>
>>> > -----Original Message-----
>>> > From: ross@aztechost.com [mailto:ross@aztechost.com]
>>> > Sent: 10 May 2007 10:08
>>> > To: mysql@lists.mysql.com
>>> > Subject: adding 3 values
>>> >
>>> >
>>> > Hi,
>>> >
>>> > I have 3 integer values in the table single_rooms, double_rooms,
>>> > twin _ooms but want to add them all up to do a comparison to see
>>> > if the combined number of rooms is less than ten.
>>> >
>>> > Ta,
>>> >
>>> > R.
>>> >
>>> >
>>>
>>> SELECT SUM(single_rooms, double_rooms, twin_rooms) from TABLE;
>>>
>>> Or, if you want a boolean value depending if there are less than 10:
>>>
>>> SELECT IF(SUM(single_rooms, double_rooms, twin_rooms) < 10,1,0)
>>> from TABLE;
>>>

>>
>> Sorry, brain was switched off when I wrote that. It should be:
>>
>> SELECT single_rooms+double_rooms+twin_rooms from TABLE;
>>
>> Or, if you want a boolean value depending if there are less than 10:
>>
>> SELECT IF((single_rooms+double_rooms+twin_rooms) < 10,1,0) from TABLE;
>>
>> Edward
>>
>> --
>> MySQL General Mailing List
>> For list archives: http://lists.mysql.com/mysql
>> To unsubscribe: http://lists.mysql.com/mysql?unsub=ross@aztechost.com
>>
>>

>
>
>

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 05:48 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com