我正在尝试从此user_id = 1查询中获取数据,但是user_id传入数据中有不同的值。
select *
from user_contacts uc
inner join contacts_email_addresses ce on uc.id=ce.contact_id
inner join contacts_phone_numbers cp on uc.id=cp.contact_id
where uc.user_id=1
and cp.user_id=1
and ce.user_id=1
and uc.contact_name like '%test%'
or uc.contact_surname like '%test%'
or ce.email_address like '%test%'
or cp.phone_number like '%test%'
Run Code Online (Sandbox Code Playgroud)
所有这些AND条件仅与第一个“或”条件一起应用(uc.contact_name如“%test%”)。使用这样的括号:
select *
from user_contacts uc
inner join contacts_email_addresses ce
on uc.id=ce.contact_id
inner join contacts_phone_numbers cp
on uc.id=cp.contact_id
where uc.user_id=1 and
cp.user_id=1 and
ce.user_id=1 and
(uc.contact_name like '%test%' or
uc.contact_surname like '%test%' or
ce.email_address like '%test%' or
cp.phone_number like '%test%')
Run Code Online (Sandbox Code Playgroud)