我有兩個查詢 - 一個通過知道post_id
來選擇group_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'
它說:「接通條款」
未知列「180」。
問題:我有那個條目!
表結構:
bio_community_posts
:
id
,user_id
,group_id
,- 其他的東西;
bio_community_group_members
:
id
,user_id
,group_id
,status
,- 其他的東西;
我需要從bio_community_group_members
檢索status
如果它存在。那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'
編輯#2:
這爲w我正在尋找的帽子:
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`
謝謝! :)
編輯#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'
但是:
[錯誤] 1054 - 未知列 '的條款' 在 'bio_community_group_members.group_id'。
編輯#4:
剛剛發現在查詢中的錯誤。這裏是最終的解決辦法:
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'
刪除反勾。例如:id = 180。 –
查看我更新的答案。 –