开发者

Query is Giving Wrong Results

开发者 https://www.devze.com 2023-03-18 04:20 出处:网络
SELECT `bio_community_groups`.`id`, `bio_community_groups`.`name`, `bio_community_groups`.`description`, `bio_开发者_如何学编程community_groups`.`members`
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

精彩评论

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