hey im writting sql query, done need change important thing still dont have idea repair it. there sql example
select post.id,post.name post_name, (select users.login users post.creator = users.id) creator, (select count(*) recommends recommends.idea_id = post.id) recommends, (select count(*) comments comments.idea_id = post.id) comments, group_concat(tags.tag separator ',') tags posts left join tags on post.id = tags.idea_id post.name in ( select post.name posts join tags on post.id = post.idea_id tags.tag in ('x','y','z') ) group post.name
what's problem? if our x or y or z not exist query still working. dont know how repair, tryed not exists clauses or not not working. help.
i looking posts have any 1 of tags, can do:
select post.id, post.name post_name, (select users.login users post.creator = users.id) creator, (select count(*) recommends recommends.idea_id = post.id) recommends, (select count(*) comments comments.idea_id = post.id) comments, group_concat(tags.tag separator ',') tags posts left join tags on post.id = tags.idea_id group post.id, post.name having sum(tags.tag in ('x', 'y', 'z')) > 0;
note: changed group by
include post.id
(assuming name unique each id
). practice include non-aggregated columns in group by
clause. if same name has multiple ids, remove post.id
select
.
if want 3 of tags and there no duplicates (a reasonable assumption), change having
to:
having sum(tags.tag in ('x', 'y', 'z')) = 3;
Comments
Post a Comment