简体   繁体   中英

Count query within select statement MySQL

I'm having a minor issue with a tiny mysql query. I'm trying to select a few columns and at the same time count what ever comes out of the table and add the count result as another column. so let's say I have three friends

  • friend 1
  • friend 2
  • friend 3

I want to see another column says allfriends=>3

So I came up with something that look like this

SELECT friends.friend, users.avatar, COUNT(*) AS allfriends FROM users,friends

now what's that doesn't work. so I tried to group them but then I got a '1' for each row. so it was just count that individual row. My question is: is there a way to count a all the results and add them all together to give me a sum of what ever i retrieved of the tables

I am not a mysql pro so this is exhasting but helped me learn a lot in the process as well. here's a sql EXAMPLE that I made with my own DB to demostrate the concept. i hope this is what you want.

the core concept is to generate a auto_increment column for each table/query results, and then left / right join depends on which table have more rows. to make a auto increamenting column within 1 SQL is fiddly, what i did is initiate the @cnt variable by

(select @cnt := 0 AS rowNumber) as t0

but i never used that table. I then do

select (@cnt := @cnt + 1) AS rowNumber, ....

in the following table for the incrmental column.

the method have a critical weakness which is you have to know which column/query will return the most row and predefine the type of JOIN in the SQL. I am not sure if this helps you in any way but I'd like to share it non the less.

mysql> select t1.rowNumber,t1.friend, t2.rowNumber,t2.avatar from (select @cnt := 0 AS rowNumber) as t0 RIGHT JOIN (select (@cnt := @cnt + 1) AS rowNumber,friend from friends) as t1 using (rowNumber) LEFT JOIN (select @cnt := 0 AS rowNumber) as t4 using (rowNumber) RIGHT JOIN (select (@cnt := @cnt + 1) AS rowNumber,avatar from user) as t2 using (rowNumber);
+-----------+-------------+-----------+------------+
| rowNumber |    friend   | rowNumber |   avatar   |
+-----------+-------------+-----------+------------+
|         1 |        0001 |         1 |       0001 |
|         2 |        0002 |         2 |       0002 |
|         3 |        0003 |         3 |       0003 |
|         4 |        0004 |         4 |       0004 |
|         5 |        0005 |         5 |       0005 |
|         6 |        0006 |         6 |       0006 |
|         7 |        0007 |         7 |       0007 |
|         8 |        0008 |         8 |       0008 |
|         9 |        0009 |         9 |       0009 |
|        10 |        0010 |        10 |       0010 |
|      NULL |        NULL |        11 |       0011 |
|      NULL |        NULL |        12 |       0012 |
|      NULL |        NULL |        13 |       0013 |
|      NULL |        NULL |        14 |       0014 |
|      NULL |        NULL |        15 |       0015 |
|      NULL |        NULL |        16 |       0016 |
|      NULL |        NULL |        17 |       0017 |
|      NULL |        NULL |        18 |       0018 |
|      NULL |        NULL |        19 |       0019 |
|      NULL |        NULL |        20 |       0020 |
+-----------+-------------+-----------+------------+
20 rows in set (0.00 sec)

This should work i think because your question is confusing little bit

SELECT friends.friend, users.avatar,xyz.* FROM users,friends,(SELECT COUNT(*) AS allfriends FROM users,friends)xyz

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM