Jump to content

[SOLVED] combining two queries.


stelthius

Recommended Posts

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.