gpt4 book ai didi

mysql - 多个 mysql 连接

转载 作者:行者123 更新时间:2023-11-29 02:34:33 25 4
gpt4 key购买 nike

我需要从三张表中获取信息,一张表中包含信息,另外两张表包含我需要计数的信息。

所以第一个表结构是:

tbl_imgimg_idimg_name
tbl_commentscomment_idimg_idcomment
tbl_votevote_idlogo_id

I want the results to have the count of comments and votes that relate to each logo.

I have a bit of the query which is for the count of comments, but have no idea for the syntax for the second join.

SELECT l.img_id, l.img_name, COUNT(c.comment_id) AS comment_count
FROM tbl_images as l
LEFT OUTER JOIN tbl_comments AS c USING (img_id);

有人能帮忙吗?

最佳答案

这个怎么样:

SELECT l.img_id, l.img_name, 
(SELECT COUNT(*) FROM tbl_comments c WHERE i.img_id = c.img_id ) AS comment_count,
(SELECT COUNT(*) FROM tbl_vote v WHERE i.img_id = v.img_id ) AS vote_count
FROM tbl_images i

关于mysql - 多个 mysql 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7308373/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com