Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 从三个表中选择_Mysql - Fatal编程技术网

Mysql 从三个表中选择

Mysql 从三个表中选择,mysql,Mysql,我试图从一个表中选择,然后根据第一个表中的行从另外两个表中计数。我尝试了下面的代码,但行总是空的 SELECT list.id, list.title, list.body, list.poster, list.created_at, count(comments.id) as comcount, count(supports.topic_id) as supcount FROM ( SELECT * FROM topics ORDER BY created_at DESC LI

我试图从一个表中选择
,然后根据第一个表中的行从另外两个表中计数。我尝试了下面的代码,但行总是空的

SELECT list.id, list.title, list.body, list.poster, list.created_at, count(comments.id) as comcount, count(supports.topic_id) as supcount
FROM (
  SELECT *
  FROM topics
  ORDER BY created_at DESC
  LIMIT 5
) AS list, comments, supports
WHERE
  list.id = comments.id OR
  list.id = supports.topic_id
在这个场景中,表
主题
只有两行,表
注释
支持
中没有行,但是我仍然可以得到两行,它们的别名分别是
supcount
comcount
,每一行都有一个值
0

我得到了上述问题的解决方案,但我正在尝试其他解决方案,我在所提供解决方案的评论区中对此进行了解释

SELECT 
t.id,
t.title,
t.body,
t.poster,
t.created_at,
s.supporter,
IFNULL((SELECT COUNT(*) FROM comments c WHERE c.id = t.id), 0) AS comcount,
IFNULL((SELECT COUNT(*) FROM supports s WHERE s.topic_id = t.id), 0) AS     supcount,
CASE WHEN (s.supporter  = "Davies Alex") THEN '1'  ELSE '0' END sup,
CASE WHEN (c.commenter  = "Davies Alex") THEN '1'  ELSE '0' END com
FROM topics t, comments c, supports s
ORDER BY created_at DESC

这将起作用,尝试一下(使用子查询只计算另一个表中的条目更合适):

更新新要求:

SELECT 
    t.id,
    t.title,
    t.body,
    t.poster,
    t.created_at,
    s.supporter,
    IFNULL(COUNT(c.id), 0) AS comcount,
    IFNULL(COUNT(s.id), 0) AS supcount,
    SUM(IF(s.supporter IS NOT NULL AND s.supporter  = "Davies Alex", 1, 0)) > 0 AS sup,
    SUM(IF(c.commenter IS NOT NULL AND c.commenter  = "Davies Alex", 1, 0)) > 0 AS com
FROM topics t
LEFT JOIN comments c ON c.id = t.id
LEFT JOIN supports s ON s.topic_id = t.id
GROUP BY t.id
ORDER BY created_at DESC

在查询中,您需要
list.id
来匹配
comments.id
supports.topic\u id
。如果使用外部联接,即使联接的表不匹配或不包含任何数据,也可以从初始表检索数据

SELECT
    topics.id, topics.title, topics.body, topics.poster, list.created_at,
    count(comments.id) as comcount,
    count(supports.topic_id) as supcount
FROM lists
LEFT JOIN comments ON comments.id = topics.id
LEFT JOIN supports ON supports.topic_id = topics.id
ORDER BY created_at DESC
LIMIT 5

这是错的吗?list.id=comments.id,comments表no中可能有list_id?@GeorgeGarchagudashvili正在尝试将列表中的
id
comments
表和
supports
表匹配,为您提供数据库模式、数据样本和预期结果,请sqlfiddle如果我想做一些更复杂的事情,例如,我想知道特定的
t.id
是否受到特定用户的支持或评论。所以我在我的新编辑中尝试了一些东西。请看一看。如果
Davies Alex
是评论/支持的作者,那么您想计算评论或支持的数量,对吗?0其他方面,评论和支持的计数是分开的。
时的
情况是确定~Davies Alex`是否对该特定主题发表了评论或支持。谢谢!你是一个很棒的职业选手!
SELECT
    topics.id, topics.title, topics.body, topics.poster, list.created_at,
    count(comments.id) as comcount,
    count(supports.topic_id) as supcount
FROM lists
LEFT JOIN comments ON comments.id = topics.id
LEFT JOIN supports ON supports.topic_id = topics.id
ORDER BY created_at DESC
LIMIT 5