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

此查询用于使用 LIKE 在用户字段上搜索用户,并使用 WHERE sql 命令在特定用户组中搜索用户。

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

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

结果中 groupid 为 3 但必须为 1

我不想在相等时使用 LIKE 命令。

WHERE `groupid` LIKE `1`

我能做些什么?

4

2 回答 2

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
于 2013-07-05T08:23:03.193 回答
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
于 2013-07-05T08:26:47.750 回答