我有两个表用户和用户组。
我正在运行以下查询: -
SELECT `u`.`id`, `u`.`fname`, `u`.`lname`, `u`.`customer_id`, `u`.`email`, `u`.`partner_id`, `u`.`campaign_promocode`, `u`.`utm_medium`, `u`.`referrral_source`, `u`.`is_active`, `u`.`created_at`, `ug`.`group_id`
FROM (`users` as u)
JOIN `users_groups` as ug ON `ug`.`user_id` = `u`.`id`
WHERE `ug`.`group_id` = '99'
OR `ug`.`group_id` = '100'
AND `u`.`is_active` IN ('1', '2')
AND `u`.`fname` LIKE '%sandeep%'
ORDER BY `u`.`id` desc
LIMIT 10
但如果我删除where子句ug
,则上述查询返回错误的结果。group_id
='99'
或者ug
。group_id
='100'然后我说得对。
数据库大约有7K行。
答案 0 :(得分:2)
试试这个:
SELECT `u`.`id`, `u`.`fname`, `u`.`lname`, `u`.`customer_id`, `u`.`email`, `u`.`partner_id`, `u`.`campaign_promocode`, `u`.`utm_medium`, `u`.`referrral_source`, `u`.`is_active`, `u`.`created_at`, `ug`.`group_id`
FROM (`users` as u)
JOIN `users_groups` as ug ON `ug`.`user_id` = `u`.`id`
WHERE (`ug`.`group_id` = '99' OR `ug`.`group_id` = '100' )
AND `u`.`is_active` IN ('1', '2')
AND `u`.`fname` LIKE '%sandeep%'
ORDER BY `u`.`id` desc
LIMIT 10
答案 1 :(得分:1)
WHERE `ug`.`group_id` = '99'
OR `ug`.`group_id` = '100'
与
不同WHERE (`ug`.`group_id` = '99'
OR `ug`.`group_id` = '100')
将where子句更改为后者。
如果你没有放括号,那么OR将查询的整个其他部分视为操作数,对于正确的结果,你必须用正确的语法给它正确的操作数。