简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM