MadTechie Posted May 28, 2009 Share Posted May 28, 2009 Yep, i just checked.. FIXED: $sql = "SELECT SUM( if(received=0,1,0 )) AS numreceived, SUM( if(unread='unread',1,0 )) AS numunread FROM messages WHERE receiver = '{$session->username}' LIMIT 1"; EDIT: someone owes me a beer Quote Link to comment https://forums.phpfreaks.com/topic/159955-solved-combining-two-queries/page/2/#findComment-843756 Share on other sites More sharing options...
stelthius Posted May 28, 2009 Author Share Posted May 28, 2009 Sorry my internet died yet again, no there is no specific reason other than consolidation sorry to cause you guys a massive head ache Quote Link to comment https://forums.phpfreaks.com/topic/159955-solved-combining-two-queries/page/2/#findComment-843779 Share on other sites More sharing options...
Ken2k7 Posted May 28, 2009 Share Posted May 28, 2009 Tested it on a test table I have. Speed-wise, it didn't seem to matter. bools = 'bunch of odd lists'... it's a test table. I have a bunch of random columns with random data. mysql> SELECT SUM( if(paid='Y',1,0 )) AS numpaid, SUM( if(userid < 50, 1,0) ) AS numid FROM bools; +---------+-------+ | numpaid | numid | +---------+-------+ | 7586 | 9095 | +---------+-------+ 1 row in set (0.00 sec) mysql> SELECT (SELECT COUNT(paid) FROM bools WHERE paid='Y') AS numpaid, (SELECT COUNT(userid) FROM bools WHERE userid < 50) AS numid; +---------+-------+ | numpaid | numid | +---------+-------+ | 7586 | 9095 | +---------+-------+ 1 row in set (0.01 sec) mysql> SELECT SUM( if(paid='Y',1,0 )) AS numpaid, SUM( if(userid < 50, 1,0) ) AS numid FROM bools; +---------+-------+ | numpaid | numid | +---------+-------+ | 7586 | 9095 | +---------+-------+ 1 row in set (0.01 sec) mysql> SELECT (SELECT COUNT(paid) FROM bools WHERE paid='Y') AS numpaid, (SELECT COUNT(userid) FROM bools WHERE userid < 50) AS numid; +---------+-------+ | numpaid | numid | +---------+-------+ | 7586 | 9095 | +---------+-------+ 1 row in set (0.01 sec) mysql> SELECT SUM( if(paid='Y',1,0 )) AS numpaid, SUM( if(userid < 50, 1,0) ) AS numid FROM bools; +---------+-------+ | numpaid | numid | +---------+-------+ | 7586 | 9095 | +---------+-------+ 1 row in set (0.00 sec) mysql> SELECT (SELECT COUNT(paid) FROM bools WHERE paid='Y') AS numpaid, (SELECT COUNT(userid) FROM bools WHERE userid < 50) AS numid; +---------+-------+ | numpaid | numid | +---------+-------+ | 7586 | 9095 | +---------+-------+ 1 row in set (0.00 sec) I would choose MadTechie's. It's much more elegant. Quote Link to comment https://forums.phpfreaks.com/topic/159955-solved-combining-two-queries/page/2/#findComment-843852 Share on other sites More sharing options...
stelthius Posted May 28, 2009 Author Share Posted May 28, 2009 Hello Ken, im using Mad's currently.. All the help given is greatly appretiated guys, Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/159955-solved-combining-two-queries/page/2/#findComment-844003 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.