我正在运行查询以检索我的数据的所有客户。由于新的法律,并非所有客户都存储了地址,因此我有一些空值。如果我运行“主”查询来检索剩下的加入另一个表的所有客户,那么它将很好地工作。当我尝试添加搜索词时,它仍然为我提供了整个集合,而不是根据搜索词对其进行过滤。我做错了什么?
我尝试不使用左联接而仅在WHERE的情况下重写查询。c.customers_id = a.customers_id,但是因为a.customers_id并不总是被填充,所以我得到的客户比我少了
select
c.customers_id, c.customers_firstname, c.customers_lastname, c.customers_email_address, c.customers_account_validated, c.customers_payment_period, a.entry_country_id, a.entry_company
from customers c
left join address_book a on c.customers_id = a.customers_id
and c.customers_default_address_id = a.address_book_id
and (c.customers_lastname like '%adolfs%'
or c.customers_firstname like '%adolfs%'
or c.customers_email_address like '%adolfs%'
or a.entry_company like '%adolfs%' or c.customers_id = 'adolfs')
order by c.customers_id DESC
我希望所有在电子邮件地址中使用“ adolfs”作为名字,姓氏,公司名称的客户的结果
答案 0 :(得分:2)
and (c.customers_lastname like '%adolfs%'
or c.customers_firstname like '%adolfs%'
or c.customers_email_address like '%adolfs%'
or a.entry_company like '%adolfs%' or c.customers_id = 'adolfs')
这些应该位于where子句中,而不是您的联接的一部分
select
c.customers_id, c.customers_firstname, c.customers_lastname,
c.customers_email_address, c.customers_account_validated,
c.customers_payment_period, a.entry_country_id, a.entry_company
from customers c
left join address_book a on c.customers_id = a.customers_id
and c.customers_default_address_id = a.address_book_id
where c.customers_lastname like '%adolfs%'
or c.customers_firstname like '%adolfs%'
or c.customers_email_address like '%adolfs%'
or a.entry_company like '%adolfs%'
or c.customers_id = 'adolfs')
group by c.customers_id
order by c.customers_id DESC
应该可以