[英]LIKE on one column and IN on another
我想編寫一個查詢,其中有兩列可以比較我的數據,但在一列上需要一個LIKE。 像這樣:
SELECT `name` , `country`
FROM details
WHERE (`email`, `phone`)
IN (
("abc@abc.com",'%123'), ("def@def.com",'%456'), ("ghi@ghi.com",'%789') )
請指導我如何編寫此查詢。 謝謝!
您可以將where
子句簡化為:
WHERE phone LIKE '%302024' AND
email in ('abc@abc.com', 'def@def.com', 'ghi@ghi.com')
編輯:
對於修改后的問題,您可以使用一系列AND
和OR
:
WHERE (phone like '%123' and email = 'abc@abc.com) OR
. . .
如果列表很長,則可能需要將值放入表中並使用join
或exists
子句。
如您所見,作為col IN ('Foo', 'Foo%')
的IN語句等效於(col = 'FOO' OR col = 'Foo%')
,這不會LIKE
那樣考慮通配符子句,為了滿足您的要求,您需要將where子句分為以下幾類:
SELECT name, country
FROM details
WHERE (phone LIKE '%123' OR phone LIKE '%456' OR phone LIKE '%789')
AND email IN ('abc@abc.com', 'def@def.com', 'ghi@ghi.com')
由於一封電子郵件可以有多個電話號碼,但是只有一個搜索,因此您將需要(或可能已經擁有)一個包含電子郵件的表格。 這樣,您將需要添加搜索字符串,然后執行一次定位而不是類似的操作。
create table emaildetails (email char(20), phonesearch char(15))
insert into emaildetails values ("abc@abc.com", "302024")
insert into emaildetails values ("def@def.com", "024")
insert into emaildetails values ("ghi@ghi.com", "15")
select d.*,ed.* from details d, emaildetails ed
where d.email in ('abc@abc.com', 'ghi@ghi.com')
and d.email = ed.email
and locate(ed.phonesearch, d.phone)>1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.