Mysql在哪里和多个像不工作

时间:2013-07-05 08:21:58

标签: mysql sql-like

SELECT * FROM (`users`) 
JOIN `groups` ON `users`.`groupid` = `groups`.`group_id` 
WHERE `groupid` = `1`
AND `first_name` LIKE '%john%' 
OR `last_name` LIKE '%john%' 
OR `username` LIKE '%john%'
OR `email` LIKE '%john%' 
LIMIT 25

使用WHERE sql命令在LIKE和特定用户组中搜索用户字段的用户的查询。

但LIKE命令覆盖WHERE。结果是;

id        groupid        firstname
===================================
2         3              john doe
5         2              johny dash

在结果中,groupid是3,但必须是1

我不想在等于使用LIKE命令。

WHERE `groupid` LIKE `1`

我该怎么办?

2 个答案:

答案 0 :(得分:1)

试试这个:

SELECT * FROM (`users`) 
JOIN `groups` ON `users`.`groupid` = `groups`.`group_id` 
WHERE `groupid` = `1`
AND (
      `first_name` LIKE '%john%' 
       OR `last_name` LIKE '%john%' 
       OR `username` LIKE '%john%'
       OR `email` LIKE '%john%' 
)
LIMIT 25

答案 1 :(得分:0)

SELECT * FROM (`users`) 
JOIN `groups` ON `users`.`groupid` = `groups`.`group_id` 
WHERE `groupid` = `1`
AND `first_name` LIKE '%john%' 
OR `last_name` LIKE '%john%' 
OR `username` LIKE '%john%'
OR `email` LIKE '%john%' 
LIMIT 25

应该是

SELECT * FROM (`users`) 
JOIN `groups` ON `users`.`groupid` = `groups`.`group_id` 
WHERE `groupid` = `1`
AND 
(
    `first_name` LIKE '%john%' 
    OR `last_name` LIKE '%john%' 
    OR `username` LIKE '%john%'
    OR `email` LIKE '%john%' 
)
LIMIT 25