开发者

Problems with query optimization

I had two queries - one that selects group_id by knowing the post_id, and one that checks that user is in that group. I tried to optimize them - put them together, but it now throws an error...

SELECT COUNT(bio_community_gr开发者_StackOverflow社区oup_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'

It says:

Unknown column '180' in 'on clause'.

Problem: I have that entry!

Table structure:

bio_community_posts:

  • id,
  • user_id,
  • group_id,
  • other stuff;

bio_community_group_members:

  • id,
  • user_id,
  • group_id,
  • status,
  • other stuff;

I need to retrieve status from bio_community_group_members if it exists. That count thing was just because I didn't knew how to start to build my query. :(

Thanks in advice.

Edit:

Hmm... now it works..... but any idea how to optimize those two queries and get one that also selects 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'

Edit #2:

This is what I was looking for:

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`

Thanks! :)

Edit #3:

I guess that i need something like this...

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'

But:

[Err] 1054 - Unknown column 'bio_community_group_members.group_id' in 'on clause'.

Edit #4:

Just found a bug in the query. Here is the ultimate solution:

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'


Remove the back-quotes(?) on 180, I think you meant '180'? Also remove single-quotes around the other column name, like this:

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'

Edit after OP edit:

I'm not exactly sure what kind of optimization you are looking for, but I'm guessing it's something like this?

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`

Edit after comments: Fixed your edit 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'


Remove the ` arround 180

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜