gpt4 book ai didi

php - MYSQL 连接查询问题

转载 作者:行者123 更新时间:2023-11-29 14:25:32 24 4
gpt4 key购买 nike

tbl_group

(group_iduser_idgroup_namegroup_imagegroup_descriptiondoi)

(9, 28, 'bbb', '1339660354imagesv.jpg', 'dfdsfsdf', '2012-06-14 13:22:34'),
(11, 1, 'cccc', '', '', '2012-06-14 14:49:56'),
(22, 1, '000', '', '', '2012-06-14 15:31:43');

tbl_groupnews

news_iduser_idgroup_idgroup_newsdoi

(1, 1, 22, 'hi \n', '2012-06-14 16:20:36'),
(2, 1, 22, 'hello', '2012-06-14 16:21:59'),
(3, 1, 22, '1111', '2012-06-14 16:25:13'),
(4, 1, 22, 'jj', '2012-06-14 16:34:41'),
(5, 28, 9, 'hi', '2012-06-15 09:48:47');

tbl_groupmembers

groupmember_idgroup_idaddermemberdoi

(1, 9, 28, 1, '2012-06-14 13:22:35'),
(2, 9, 28, 66, '2012-06-14 13:22:35'),
(4, 11, 1, 2, '2012-06-14 14:49:56'),
(5, 11, 1, 28, '2012-06-14 14:49:56'),
(6, 11, 1, 62, '2012-06-14 14:49:56'),
(36, 22, 1, 28, '2012-06-14 16:15:41'),
(37, 22, 1, 62, '2012-06-14 16:16:55'),
(38, 22, 1, 66, '2012-06-14 16:18:35'),
(39, 22, 1, 70, '2012-06-14 16:19:33');

我运行查询

  SELECT tbl_groups.*,
COUNT (tbl_groupnews.news_id) AS cn1,
COUNT (tbl_groupmembers.group_id) AS cn2
FROM tbl_groups
LEFT JOIN tbl_groupnews ON tbl_groups.group_id = tbl_groupnews.group_id
LEFT JOIN tbl_groupmembers ON tbl_groupmembers.group_id = tbl_groups.group_id
WHERE (tbl_groups.user_id = 28 OR tbl_groupmembers.member = 28)
GROUP BY tbl_groups.group_id

我的要求是我需要从组中获取所有值、新闻数量和成员数量WHERE user_id = 28 或member id = 28

谁能帮我找到解决办法吗?

最佳答案

尝试这样:

SELECT tbl_groups.*,          
COUNT (tbl_groupnews.news_id) AS cn1,
COUNT (tbl_groupmembers.group_id) AS cn2
FROM tbl_groups
LEFT JOIN tbl_groupnews ON tbl_groups.group_id = tbl_groupnews.group_id
AND tbl_groups.user_id = 28
LEFT JOIN tbl_groupmembers ON tbl_groupmembers.group_id = tbl_groups.group_id
AND tbl_groupmembers.MEMBER = 28
GROUP BY tbl_groups.group_id

关于php - MYSQL 连接查询问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11045951/

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