Buyocat Posted June 30, 2006 Share Posted June 30, 2006 Can anyone tell me how to construct the following statment so it works? Currently I get an error - at or around the "and" portion of it.[code]SELECT SUM(IF(s.started >= 1140211367 AND s.ended <= 1151779520)) as num_logs FROM fp_session_history s[/code]I have tried using && as well with no luck. Link to comment https://forums.phpfreaks.com/topic/13328-using-an-if-clause-in-mysql/ Share on other sites More sharing options...
Barand Posted June 30, 2006 Share Posted June 30, 2006 Perhaps, if I knew what you were tryin to do Link to comment https://forums.phpfreaks.com/topic/13328-using-an-if-clause-in-mysql/#findComment-51398 Share on other sites More sharing options...
shocker-z Posted June 30, 2006 Share Posted June 30, 2006 look at http://dev.mysql.com/doc/refman/5.1/en/partitioning-range.htmlSELECT SUM(IF(s.started >= 1140211367 AND s.ended <= 1151779520)) as num_logs FROM fp_session_historyyou would not use as num_logs either as that wouldn't hold a value.. Link to comment https://forums.phpfreaks.com/topic/13328-using-an-if-clause-in-mysql/#findComment-51399 Share on other sites More sharing options...
Barand Posted June 30, 2006 Share Posted June 30, 2006 OK - guessing game - are you trying to count the rows where (s.started >= 1140211367 AND s.ended <= 1151779520) ?if so,SELECT COUNT(*) WHERE s.started >= 1140211367 AND s.ended <= 1151779520Are you trying to get the total of another column depending on the IF condition. If so the IF syntax is IF(condition, value if true, value if false)SELECT SUM(IF(s.started >= 1140211367 AND s.ended <= 1151779520), anothercol, 0 ) as num_logs FROM fp_session_history s Link to comment https://forums.phpfreaks.com/topic/13328-using-an-if-clause-in-mysql/#findComment-51402 Share on other sites More sharing options...
Buyocat Posted June 30, 2006 Author Share Posted June 30, 2006 Thanks for the hand fellas, Barand I ended up using what you suggested, although I discovered that this works and gets the same result...[code]SELECT SUM(IF(time1 >= timeA && time2 <= timeB), 1, 0) ...[/code](don't ask me what me the 1 and 0 do, I think that it adds one if the if is true and zero otherwise) At any rate that fetches the same result as[code]SELECT COUNT(*) ... WHERE time1 > sometime AND time2 < someothertime[/code]I went with count, it just seems more readable. Link to comment https://forums.phpfreaks.com/topic/13328-using-an-if-clause-in-mysql/#findComment-51423 Share on other sites More sharing options...
Barand Posted June 30, 2006 Share Posted June 30, 2006 I'd go with the COUNT(*) WHERE option.The times I'd use IF is when a client wants a weekly report like[pre]Product Monday Tuesday .... Friday Weekly_Total[/pre]Then my query would be something likeSELECT product, SUM(IF weekday=1, sales, 0) as monday, SUM(IF weekday=2, sales, 0) as tuesday... etc Link to comment https://forums.phpfreaks.com/topic/13328-using-an-if-clause-in-mysql/#findComment-51449 Share on other sites More sharing options...
.josh Posted July 1, 2006 Share Posted July 1, 2006 if barand makes a suggestion about a sql query, you'd be wise to listen. Link to comment https://forums.phpfreaks.com/topic/13328-using-an-if-clause-in-mysql/#findComment-51543 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.