我有两个查询 - 一个通过了解group_id
选择post_id
,另一个检查该用户是否在该组中。我试图优化它们 - 把它们放在一起,但它现在抛出一个错误......
SELECT COUNT(bio_community_group_members.id) AS count
FROM `bio_community_group_members`
JOIN `bio_community_posts`
ON (`bio_community_posts`.`id` = `180`)
WHERE `bio_community_group_members`.`group_id` = 'bio_community_posts.group_id'
AND `bio_community_posts`.`user_id` = '34'
它说:
'on子句'中的未知列'180'。
问题:我有这个条目!
表格结构:
bio_community_posts
:
id
,user_id
,group_id
, bio_community_group_members
:
id
,user_id
,group_id
,status
,如果status
存在,我需要从bio_community_group_members
检索count
。 status
这件事只是因为我不知道如何开始构建我的查询。 :(
感谢您的建议。
修改
嗯...现在它可以工作.....但是任何想法如何优化这两个查询并得到一个也选择SELECT `group_id`
FROM `bio_community_posts`
WHERE `id` = 180
SELECT COUNT(id) AS count
FROM `bio_community_group_members`
WHERE `group_id` = 41
AND `user_id` = '34'
?
SELECT `bio_community_group_members`.`status`
FROM `bio_community_group_members`
JOIN `bio_community_posts` ON `bio_community_posts`.`group_id` = `bio_community_group_members`.`group_id`
WHERE `bio_community_group_members`.`group_id` = 41
AND `bio_community_group_members`.`user_id` = '34'
AND `bio_community_posts`.`id` = '180'
GROUP BY `bio_community_group_members`.`status`
编辑#2:
这就是我想要的:
SELECT `bio_community_group_members`.`status`
FROM `bio_community_group_members`
JOIN `bio_community_posts` ON `bio_community_posts`.`group_id` = `bio_community_group_members.group_id`
WHERE `bio_community_posts`.`id` = '180'
AND `bio_community_posts`.`user_id` = '34'
谢谢! :)
编辑#3:
我想我需要这样的东西......
SELECT `bio_community_group_members`.`status`
FROM `bio_community_group_members`
JOIN `bio_community_posts` ON `bio_community_posts`.`group_id` = `bio_community_group_members`.`group_id`
WHERE `bio_community_posts`.`id` = '180'
AND `bio_community_posts`.`user_id` = '34'
可是:
[Err] 1054 - 'on clause'中的未知列'bio_community_group_members.group_id'。
编辑#4:
刚刚在查询中发现了一个错误。这是最终的解决方案:
{{1}}
答案 0 :(得分:4)
在180上删除后引号(?),我认为你的意思是'180'?同时删除其他列名称周围的单引号,如下所示:
SELECT COUNT(bio_community_group_members.id) AS count
FROM `bio_community_group_members`
JOIN `bio_community_posts`
ON (`bio_community_posts`.`id` = '180') -- Replaced back- with single-quotes.
WHERE `bio_community_group_members`.`group_id` = `bio_community_posts.group_id` -- Replaced single- with back-quotes.
AND `bio_community_posts`.`user_id` = '34'
OP编辑后编辑:
我不确定你在寻找什么样的优化,但我猜它是这样的?
SELECT COUNT(id) AS count, status
FROM `bio_community_group_members`
JOIN `bio_community_posts` ON `bio_community_posts`.`group_id` = `bio_community_group_members`.`group_id`
WHERE `bio_community_group_members`.`group_id` = 41
AND `bio_community_group_members`.`user_id` = '34'
AND `bio_community_posts`.`id` = '180'
GROUP BY `bio_community_group_members`.`status`
评论后修改: 修正了你的编辑3:
SELECT `bio_community_group_members`.`status`
FROM `bio_community_group_members`
JOIN `bio_community_posts` ON `bio_community_posts`.`group_id` = `bio_community_group_members`.`group_id`
WHERE `bio_community_posts`.`id` = '180'
AND `bio_community_posts`.`user_id` = '34'
答案 1 :(得分:1)
删除`arround 180
ON (`bio_community_posts`.`id` = 180)