[英]Count query within select statement MySQL
I'm having a minor issue with a tiny mysql query. 我在使用小型mysql查询时遇到了一个小问题。 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
所以说我有三个朋友
I want to see another column says allfriends=>3 我想看到另一列说所有朋友=> 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.
所以我尝试将它们分组,但随后每行得到一个“ 1”。 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. 我不是mysql专业人士,所以这令人厌烦,但同时也帮助我学到了很多东西。 here's a sql EXAMPLE that I made with my own DB to demostrate the concept.
这是我用自己的数据库制作的一个SQL示例,以演示该概念。 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. 核心概念是为每个表/查询结果生成一个auto_increment列,然后左/右联接取决于哪个表具有更多行。 to make a auto increamenting column within 1 SQL is fiddly, what i did is initiate the @cnt variable by
要在1个SQL中自动创建自动列,我所做的是通过以下方式初始化@cnt变量
(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. 该方法的一个关键弱点是您必须知道哪个列/查询将返回最多的行并预定义SQL中的JOIN类型。 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 SELECT friends.friend,users.avatar,xyz。* FROM users,friends,(SELECT COUNT(*)AS allfriends from users,friends)xyz
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.