[英]SQL left join with clause where on right table
I need to request customer on database with multiple criteria. 我需要在具有多个条件的数据库上请求客户。
In my database, I have 3 tables 在我的数据库中,我有3个表
Customer : 客户:
id
nom
prenom
...
Contact : 联系 :
id
valeur
customer_id
In my criteria of request, I have : valeur 根据我的要求,我有:valeur
This is the request : 这是请求:
select client0_.id as id1_6_
from client client0_
inner join entite entite1_ on client0_.entite_id=entite1_.id
left outer join client_adresse clientadre2_ on (clientadre2_.client_id=client0_.id)
left outer join client_contact clientcont3_ on (clientcont3_.client_id=client0_.id)
where (entite1_.numero_licence in ('ZB230EC'))
and (upper(client0_.nom) like ('%'||upper('')||'%'))
and (client0_.prenom is null or upper(client0_.prenom) like ('%'||upper('')||'%'))
and (clientcont3_.valeur like ('%'||'@mail'||'%'))
and (client0_.date_creation_sys is null or client0_.date_creation_sys>='2017-01-01')
and (client0_.date_modification_sys is null or client0_.date_modification_sys>='2017-01-01')
and (client0_.date_suppression is null or client0_.date_suppression>='9999-12-12')
This request return me customer which have not contact also my request ask customer with contact "valeur" contain "@mail" 此请求还给我还没有联系过的客户,我的请求又请联系“ valeur”的客户包含“ @mail”
Sample data 样本数据
Customer 顾客
id | nom | prenom
375 | aurelie | lilie
389 | t( |
In Contact 接触
id | valeur | customer_id
2740 | aurelie@mail.com | 375
2739 | 06 06 06 06 06 | 375
If I launch request with search : 如果我通过搜索启动请求:
contact.valeur like '%@mail%'
and customer.nom like '%%'
My result is OK 我的结果还可以
But with : 但是用:
contact.valeur like '%%'
and customer.nom like '%t(%'
I have empty result 我没有结果
Can you help me? 你能帮助我吗?
There are some pieces that explain this: 有一些片段可以解释这一点:
Therefore, if you want to get nothing, just move your filter in the "join part" to the "where part". 因此,如果您什么也不想得到,只需将“连接部分”中的过滤器移至“位置部分”即可。
Edited answer 编辑答案
Just to explain my comment... 只是为了解释我的评论...
with customer as (
select 375 as id, 'aurelie' as nom, 'lilie' as prenom union all
select 389, 't(', null
),
contact as (
select 2740 as id, 'aurelie@mail.com' as valeur, 375 customer_id union all
select 2739, '06 06 06 06 06', 375
)
select *
from customer cs
left join contact c
on cs.id=c.customer_id
where c.valeur like '%%'
and cs.nom like '%t(%'
This return nothing, because you are comparing NULL with like '%%'
这没有返回任何值,因为您正在将NULL与
like '%%'
进行比较
If you replace this where c.valeur like '%%'
by this where c.valeur is null
, you will get the record you are looking for. 如果
where c.valeur is null
where c.valeur like '%%'
替换为where c.valeur like '%%'
,则将获得您要查找的记录。 If you want to continue using this where c.valeur like '%%'
always, you will need to convert the nulls to some string, for example an empty string where isnull(c.valeur,'') like '%%'
and it should work too. 如果要继续使用
where c.valeur like '%%'
始终where c.valeur like '%%'
这样的位置,则需要将null转换为某些字符串,例如where isnull(c.valeur,'') like '%%'
的空字符串。它也应该工作。 Hope it helps, you can test it from here 希望对您有所帮助,您可以从这里进行测试
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.