如何获得所有相关行数的行?

时间:2019-04-07 07:16:55

标签: mysql sql

我有一个像这样的表结构:

// question_and_answer
+----+-----------+------------+
| id |   title   | related_id |
+----+-----------+------------+
| 1  | q1        | NULL       |
| 2  |           | 1          |
| 3  |           | 1          |
| 4  | q2        | NULL       |
| 5  |           | 1          |
| 6  |           | 4          |
| 7  |           | 1          |
| 8  | q3        | NULL       |
| 9  |           | 8          |
| 10 |           | 4          |
+----+-----------+------------+

// votes
+----+---------+
| id | post_id |
+----+---------+
| 1  | 2       |
| 2  | 2       |
| 3  | 1       |
| 4  | 6       |
| 5  | 2       |
| 6  | 1       |
| 7  | 8       |
+----+---------+

我需要得到三件事:

  1. 一行标题(给定ID)
  2. 其相关行数
  3. 其票数

我可以通过三个独立的查询来获取它们:

// Assuming id = 1 

SELECT title FROM question_and_answer WHERE id = 1;

SELECT count(a.*) FROM question_and_answer q 
LEFT JOIN question_and_answer a ON q.id = a.related_id WHERE q.id = 1; 

SELECT count(v.*) FROM question_and_answer qa
LEFT JOIN votes v on qa.id = v.post_id
WHERE qa.id = 1;

预期结果是这样

// Assuming id = 1 
+-------+-------------+-----------+
| title | answers_num | votes_num |
+-------+-------------|-----------+
| q1    | 4           | 2         |
+-------+-------------+-----------+

// Assuming id = 2
+-------+-------------+-----------+
| title | answers_num | votes_num |
+-------+-------------|-----------+
|       | 0           | 3         |
+-------+-------------+-----------+

有什么主意,我怎么能用一个查询得到它?

1 个答案:

答案 0 :(得分:1)

要获取不为空值的行数,可以使用count和左连接 对于单个ID,例如:1

SELECT qa.title, t1.answers_num,  t2.votes_num
FROM question_and_answer qa 
LEFT  JOIN (
  SELECT qa.title, count(c.related_id) answers_num
  FROM question_and_answer qa 
  LEFT  JOIN question_and_answer c ON qa.id = c.related_id
  GROUP BY qa.title
) t1  on t1.title = qa.title 
LEFT JOIN (
    SELECT qa.title, count(v.post_id) votes_num
    FROM question_and_answer qa 
    LEFT  JOIN votes v on qa.id = v.post_id
    GROUP BY qa.title
) t2 ON t2.title = qa.title 
WHERE qa.id = 1;

对于所有ID

SELECT qa.title, t1.answers_num,  t2.votes_num
FROM question_and_answer qa 
LEFT  JOIN (
  SELECT qa.title, count(c.related_id) answers_num
  FROM question_and_answer qa 
  LEFT  JOIN question_and_answer c ON qa.id = c.related_id
) t1  on t1.title = qa.title 
LEFT JOIN (
    SELECT qa.title, count(v.post_id) votes_num
    FROM question_and_answer qa 
    LEFT  JOIN votes v on qa.id = v.post_id
) t2 ON t2.title = qa.title 

对于obatin,可以使用不同的非null值计数

对于单个ID,例如:1

SELECT qa.title, c.count(distinct related_id) answers_num
,  v.count(distinct post_id) votes_num
FROM question_and_answer qa 
LEFT  JOIN question_and_answer c ON qa.id = c.related_id
LEFT  JOIN votes v on qa.id = v.post_id
WHERE qa.id = 1;

对于所有ID

SELECT qa.title, c.count(distinct related_id) answers_num
,  v.count(distinct post_id) votes_num
FROM question_and_answer qa 
LEFT  JOIN question_and_answer c ON qa.id = c.related_id
LEFT  JOIN votes v on qa.id = v.post_id
WHERE related_id is null  
group by qa.title;