2011-07-21 95 views
1

我有兩個查詢 - 一個通過知道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' 
+1

刪除反勾。例如:id = 180。 –

+0

查看我更新的答案。 –

回答

4

刪除反引號的180,我想你的意思是 '180'(?)?同時周圍的其他列名中刪除單引號,像這樣:OP編輯後

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' 

編輯:

我不完全相信你在找什麼樣的優化情況,但我米猜猜這是這樣的?評論後

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' 
+0

請參閱我的編輯。 – daGrevis

+0

謝謝,看我的編輯。 – daGrevis

+0

嗯......該死的。不完全是。我只需要知道user_id和post_id就知道用戶狀態。只有一個查詢可能嗎? – daGrevis

1

刪除`角落找尋180

ON (`bio_community_posts`.`id` = 180)