简体   繁体   English

一列为LIKE,另一列为IN

[英]LIKE on one column and IN on another

I want to write a query where I have two columns to compare my data from but need a LIKE on one column. 我想编写一个查询,其中有两列可以比较我的数据,但在一列上需要一个LIKE。 Something like this: 像这样:

SELECT `name` , `country` 
FROM details
WHERE (`email`, `phone`)
IN (
("abc@abc.com",'%123'), ("def@def.com",'%456'), ("ghi@ghi.com",'%789') )

Please guide me on how I would write this query. 请指导我如何编写此查询。 Thanks! 谢谢!

You would simplify the where clause to: 您可以将where子句简化为:

WHERE phone LIKE '%302024' AND
      email in ('abc@abc.com', 'def@def.com', 'ghi@ghi.com')

EDIT: 编辑:

For your revised question, you can use a series of AND and OR : 对于修改后的问题,您可以使用一系列ANDOR

WHERE (phone like '%123' and email = 'abc@abc.com) OR
      . . .

If the list is long, you might want to put the values into a table and use a join or exists clause. 如果列表很长,则可能需要将值放入表中并使用joinexists子句。

an IN statment as col IN ('Foo', 'Foo%') is equivalent to (col = 'FOO' OR col = 'Foo%') as you can see, that would not take into account the wildcard as in the LIKE clause, to achieve your requirements, you will need to divide your where clause as the following: 如您所见,作为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')

Since one email can have multiple phone numbers but one search, you will need (or probably already have) a table with emails. 由于一封电子邮件可以有多个电话号码,但是只有一个搜索,因此您将需要(或可能已经拥有)一个包含电子邮件的表格。 In that you will need to add the search string and then do a locate instead of a like. 这样,您将需要添加搜索字符串,然后执行一次定位而不是类似的操作。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM