Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.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,我总共有6个表,其中保存了不同的信息 现在我需要一个从5个表中获取计数并从主表中选择所有信息的结果,但如果记录不存在,则必须返回0,而不是找不到行,这就是问题所在 我尝试了下面的查询,但没有成功 SELECT u.*, COUNT(DISTINCT c.id) as comments, COUNT(DISTINCT d.id) as dislikes, COUNT(DISTINCT l.id) as likes, COUNT(DISTINCT s.id

我总共有6个表,其中保存了不同的信息

现在我需要一个从5个表中获取计数并从主表中选择所有信息的结果,但如果记录不存在,则必须返回0,而不是找不到行,这就是问题所在

我尝试了下面的查询,但没有成功

SELECT 
    u.*,
    COUNT(DISTINCT  c.id) as comments,
    COUNT(DISTINCT d.id) as dislikes,
    COUNT(DISTINCT l.id) as likes,
    COUNT(DISTINCT s.id) as shares,
    COUNT(DISTINCT t.id) as tags
FROM 
    job_details as u
    JOIN job_comments as c ON u.id = c.job_id
    JOIN job_dislike as d ON u.id = d.job_id
    JOIN job_like as l ON u.id = l.job_id
    JOIN job_share as s ON u.id = s.job_id
    JOIN job_tags as t ON u.id = t.job_id
WHERE 
    u.id = c.job_id AND
    u.id = d.job_id AND
    u.id = l.job_id AND
    u.id = s.job_id AND
    u.id = t.job_id

GROUP BY 
    u.id
已执行此查询,但未得到确切结果。 我不太明白为什么

我希望这里有人能帮我


谢谢

使用
左连接
而不是
连接
。而且您不需要
WHERE
子句,因为您已经加入了这些表。并且,使用
IFNULL
函数为空值返回0。您需要修改查询,如下所示:

SELECT u.id,
    IFNULL(COUNT(DISTINCT  c.id),0) as comments,
    IFNULL(COUNT(DISTINCT d.id),0) as dislikes,
    IFNULL(COUNT(DISTINCT l.id),0) as likes,
    IFNULL(COUNT(DISTINCT s.id),0) as shares,
    IFNULL(COUNT(DISTINCT t.id),0) as tags
FROM job_details as u
    LEFT JOIN job_comments as c ON u.id = c.job_id
    LEFT JOIN job_dislike as d ON u.id = d.job_id
    LEFT JOIN job_like as l ON u.id = l.job_id
    LEFT JOIN job_share as s ON u.id = s.job_id
    LEFT JOIN job_tags as t ON u.id = t.job_id
GROUP BY u.id

您可能没有得到确切的结果,因为某些表可能缺少值

虽然您可以通过
左连接解决此问题,但更安全的解决方案是预聚合数据:

SELECT u.*, c.comments, d.dislikes, l.likes, s.shares, t.tags
FROM job_details as u LEFT JOIN
     (select c.job_id, count(*) as comments from job_comments group by c.job_id
     ) c
     ON u.id = c.job_id LEFT JOIN
     (select d.job_id, count(*) as dislikes from job_dislike d group by d.job_id
     ) d
     ON u.id = d.job_id LEFT JOIN
     (select l.job_id, count(*) as likes from job_like l group by l.job_id
     ) l
     ON u.id = l.job_id LEFT JOIN
     (select s.job_id, count(*) as shares from job_share s group by s.job_id
     ) s
     ON u.id = s.job_id LEFT JOIN
     (select t.job_id, count(*) as tags from job_tags t group by t.job_id
     ) t
     ON u.id = t.job_id;

为什么这样更好?考虑一个ID有5个评论,喜欢,不喜欢,股票和标签。
JOIN
方法生成中间结果,中间行数为5*5*5*5*5=3125。对于流行的ID,情况可能真的会失控。

发布一些示例数据、实际结果和预期结果。由于存在内部联接,您可能会获得比预期更少的数据…您应该检查哪个是主表,并使用左联接联接其他表。是否可以在此查询中添加where条件?@AnkitDoshi。对在外部查询中或在
on子句中,具体取决于您希望发生什么。