简体   繁体   English

在多个列中检查多个值,SQL选择

[英]Checking for multiple values in multiple columns, sql select

I have a pretty SQL select query that works as follows: 我有一个漂亮的SQL选择查询,其工作方式如下:

select * from session
where date(ts) = '2017-10-16'
and 7200 in (callingpartyno,finallycalledpartyno);

This retrieves 24 records as it should. 这样可以检索24条记录。 The problem now is that I have 14 other extensions to check for within those same 2 columns and I don't know how to look for multiple values within multiple columns. 现在的问题是,我还有14个其他扩展要在相同的2列中进行检查,而我不知道如何在多个列中查找多个值。

Let's say the previous query returns 24 and I do the same query with 7201 and it returns 6 rows. 假设上一个查询返回24,而我对7201做同样的查询,则返回6行。 I want a way that would return 30 rows in that case. 我想要一种在这种情况下将返回30行的方法。 Same goes for 7200 through 7213, so to pull any row with that time stamp that has ANY of those 13 extensions in EITHER of those columns. 7200到7213也是一样,因此要拉出带有该列的EITHER中13个扩展中的任何一个的时间戳的任何行。

Is there a way to do this? 有没有办法做到这一点?

I tried like this: 我这样尝试过:

select * from ambition.session 
where date(ts) = '2017-10-16'
and 7276 in (CALLINGPARTYNO, FINALLYCALLEDPARTYNO)
and 7314 in (CALLINGPARTYNO, FINALLYCALLEDPARTYNO)
and 7295 in (CALLINGPARTYNO, FINALLYCALLEDPARTYNO)
and 7306 in (CALLINGPARTYNO, FINALLYCALLEDPARTYNO)
and 7357 in (CALLINGPARTYNO, FINALLYCALLEDPARTYNO)
and 7200 in (CALLINGPARTYNO, FINALLYCALLEDPARTYNO) 
and 7218 in (CALLINGPARTYNO, FINALLYCALLEDPARTYNO)
and 7247 in (CALLINGPARTYNO, FINALLYCALLEDPARTYNO)
and 7331 in (CALLINGPARTYNO, FINALLYCALLEDPARTYNO) 
and 7255 in (CALLINGPARTYNO, FINALLYCALLEDPARTYNO)
and 7330 in (CALLINGPARTYNO, FINALLYCALLEDPARTYNO)
and 7000 in (CALLINGPARTYNO, FINALLYCALLEDPARTYNO)
and 7215 in (CALLINGPARTYNO, FINALLYCALLEDPARTYNO)
and 7240 in (CALLINGPARTYNO, FINALLYCALLEDPARTYNO)
and 7358 in (CALLINGPARTYNO, FINALLYCALLEDPARTYNO)
and 7312 in (CALLINGPARTYNO, FINALLYCALLEDPARTYNO);

but I get no records at all 但是我什么都没有

I really think this would more commonly be written as: 我真的认为这通常写为:

where date(ts) = '2017-10-16' and
      (CALLINGPARTYNO in (7276, . . .) or  -- I think the OP wants either one to match
       FINALLYCALLEDPARTYNO in (7276, . . .)
      );

Use and if each should match an extension. 使用and如果每个都应匹配扩展名。 Use or if either one needs to match. 如果需要匹配,请使用or

I am fairly certain there is not any specific syntax for checking for "intersections" like that, but this would be simplest. 我相当确定没有像这样的“交集”检查的任何特定语法,但这将是最简单的。

... AND (callingpartyno IN (the list) OR finallycalledpartyno IN (the list))

Sidenote: Using almost any function in a WHERE condition, like DATE() , will kill your query's performance; 旁注:在WHERE条件下使用几乎任何函数,例如DATE() ,都会破坏查询的性能; ts >= '2017-10-16 00:00:00' AND ts < '2017-10-17 00:00:00' is usually a much better choice. ts >= '2017-10-16 00:00:00' AND ts < '2017-10-17 00:00:00'通常是一个更好的选择。

You can use a subquery: 您可以使用子查询:

SELECT *
FROM ambition.session s
  CROSS JOIN (
      SELECT 7276 e UNION
      SELECT 7314 UNION
      SELECT 7295 UNION
      SELECT 7306 UNION
      SELECT 7357 UNION
      SELECT 7200 UNION 
      SELECT 7218 UNION
      SELECT 7247 UNION
      SELECT 7331 UNION 
      SELECT 7255 UNION
      SELECT 7330 UNION
      SELECT 7000 UNION
      SELECT 7215 UNION
      SELECT 7240 UNION
      SELECT 7358 UNION
      SELECT 7312) ext
WHERE date(ts) = '2017-10-16'
AND e.ext IN (CALLINGPARTYNO, FINALLYCALLEDPARTYNO)
;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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