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: ...
| |||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| > > -----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 |
| |||
| 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 > > |
| ||||
| >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 >> >> > > > |