[英]mysql update. count, rows where column is null and a columns is the same
[英]MySQL Count Where Two Columns are Same and Not Null, Same Table
我有一个包含以下数据的表:
mysql> describe Post;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| user_id | int(11) | NO | MUL | NULL | |
| post_date | datetime | NO | | NULL | |
| in_reply_to | int(11) | YES | | NULL | |
| text | varchar(160) | NO | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
mysql> select id as "Row ID", user_id as "User ID", post_date as "Post Date", IF(in_reply_to is NULL, "None", in_reply_to) as "In Reply To Post ID:", CONCAT(LEFT(text,40),"...") as "Post Text" from Post;
+--------+---------+---------------------+----------------------+---------------------------------------------+
| Row ID | User ID | Post Date | In Reply To Post ID: | Post Text |
+--------+---------+---------------------+----------------------+---------------------------------------------+
| 1 | 1 | 2015-08-14 20:38:00 | None | This is the original test post that I pu... |
| 2 | 2 | 2015-08-14 20:39:00 | None | This is the second post that I put into ... |
| 3 | 5 | 2015-08-14 22:00:00 | 1 | Hahaha, that post was hilarious. I canno... |
| 4 | 4 | 2015-08-14 23:00:00 | 1 | Today I saw a cat jump off the roof, ont... |
| 5 | 4 | 2015-08-14 23:00:00 | None | Today I saw a cat jump off the roof, ont... |
| 27 | 1 | 2015-09-08 05:53:40 | 2 | This is a mad reply ay... |
| 28 | 1 | 2015-09-08 11:24:05 | None | Yolo Swag... |
+--------+---------+---------------------+----------------------+---------------------------------------------+
7 rows in set (0.05 sec)
如果您不确定各列所代表的内容,则每个列都有说明。 我对此问题关注的两列是id
和in_reply_to
。
in_reply_to
是一个NULLABLE
FK整数,它引用同一表中的id
。 如果in_reply_to
为NULL
,则表示该帖子是原始帖子;如果它是整数值,则它是一个回复帖子,并表示该帖子所回复的ID。
在下面的示例中,有4个原始帖子(1、2、5、28)和3个回复(3、4、27),即3是对1的回复,4是对1的回复,而27是对1的回复到2。我正在寻找一个执行如下查询的SQL查询:
其中,“ Num Replies
是同一表中in_reply_to
等于id
行的COUNT
方式; 如果对该帖子没有回复,则显示0
(即,没有行包含特定帖子的ID作为其in_reply_to
列。
谢谢。
解决方案(根据安德斯的回答):
mysql> SELECT Post.id, Post.user_id, Post.post_date, Post.in_reply_to, CONCAT(LEFT(Post.text,40)), IF(counts.count IS NULL, 0, counts.count) AS 'Num of Replies' FROM Post LEFT JOIN (SELECT in_reply_to AS id, COUNT(*) AS count FROM Post WHERE in_reply_to IS NOT NULL GROUP BY in_reply_to) AS counts ON Post.id = counts.id;
+----+---------+---------------------+-------------+------------------------------------------+----------------+
| id | user_id | post_date | in_reply_to | CONCAT(LEFT(Post.text,40)) | Num of Replies |
+----+---------+---------------------+-------------+------------------------------------------+----------------+
| 1 | 1 | 2015-08-14 20:38:00 | NULL | This is the original test post that I pu | 2 |
| 2 | 2 | 2015-08-14 20:39:00 | NULL | This is the second post that I put into | 1 |
| 3 | 5 | 2015-08-14 22:00:00 | 1 | Hahaha, that post was hilarious. I canno | 0 |
| 4 | 4 | 2015-08-14 23:00:00 | 1 | Today I saw a cat jump off the roof, ont | 0 |
| 5 | 4 | 2015-08-14 23:00:00 | NULL | Today I saw a cat jump off the roof, ont | 0 |
| 27 | 1 | 2015-09-08 05:53:40 | 2 | This is a mad reply ay | 0 |
| 28 | 1 | 2015-09-08 11:24:05 | NULL | Random Text | 0 |
+----+---------+---------------------+-------------+------------------------------------------+----------------+
7 rows in set (0.00 sec)
您需要在同一张表上联接两个查询。 第一个选择所有帖子,第二个选择每个帖子的回复数。 这是左联接,因为您希望包括没有任何回复的帖子(不会从第二个查询中返回)。 IF
在那里将这些值的NULL
值转换为0
。
SELECT
post.id,
-- Other fields...,
IF(counts.count IS NULL, 0, counts.count) AS count
FROM post
LEFT JOIN
(SELECT
in_reply_to AS id,
COUNT(*) AS count
FROM post
WHERE in_reply_to IS NOT NULL
GROUP BY in_reply_to) AS counts
ON post.id = counts.id
免责声明:我尚未对此进行测试。
您可以采用传统方式进行联接,也可以直接在新列中进行联接。
例:
select a.id,
(select count(*) from (select 1 as id union all select 1 union all select 2)b where b.id=a.id) as count_of_replies
from
(select 1 as id union all select 1 union all select 2)a
请注意2个子查询“表”都是同一个表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.