开发者

Query is Giving Wrong Results

SELECT `bio_community_groups`.`id`, `bio_community_groups`.`name`, `bio_community_groups`.`description`, `bio_开发者_如何学编程community_groups`.`members`
FROM `bio_community_groups`
WHERE `bio_community_groups`.`category_id` = '1'
AND `bio_community_groups`.`name` LIKE '%rock%'
OR `bio_community_groups`.`description` LIKE '%rock%'

Problem: there isn't group with ID = 1, but anyway... it gives me all groups where name or description is like '%rock%'.

Maybe brackets may help me? Where should I put them?


Perhaps this is what you might be looking for :

SELECT `bio_community_groups`.`id`, `bio_community_groups`.`name`, `bio_community_groups`.`description`, `bio_community_groups`.`members`
FROM `bio_community_groups`
WHERE
( `bio_community_groups`.`category_id` = '1' )

AND 
( `bio_community_groups`.`name` LIKE '%rock%'
OR `bio_community_groups`.`description` LIKE '%rock%' );

In your original query, you will get results satisfying :

`bio_community_groups`.`description` LIKE '%rock%

whatever the category_id may be .


AND precedes OR in MySQL. so your query is like (bio_community_groups.category_id = '1' AND bio_community_groups.name LIKE '%rock%') OR (bio_community_groups.description LIKE '%rock%). Just place the appropriate brackets to resolve this

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜