(group_id
,user_id
,group_name
,group_image
,group_description
,doi
)
(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');
news_id
,user_id
,group_id
,group_news
,doi
(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_id
,group_id
,adder
,member
,doi
(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 or member id = 28
。
任何人都可以帮我找到解决方案吗?
答案 0 :(得分:1)
试试这样:
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
答案 1 :(得分:0)
如果您从tbl_groups中选择所有列,则它们必须位于您的分组语句
中SELECT tbl_groups.*,COUNT(distinct tbl_groupnews.news_id) as cn1,
COUNT(distinct 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,tbl_groups.user_id, tbl_groups.group_name, tbl_groups.group_image, tbl_groups.group_description, tbl_groups.doi