我有一个漂亮的SQL select查询,其工作原理如下:
select * from session
where date(ts) = '2017-10-16'
and 7200 in (callingpartyno,finallycalledpartyno);
这将检索24条记录。现在的问题是我有14个其他扩展要在同一个2列中检查,我不知道如何在多个列中查找多个值。
假设前一个查询返回24并且我使用7201
执行相同的查询,并返回6行。我想要一种在这种情况下会返回30行的方法。对于7200到7213也是如此,所以要使用那些在这些列的EITHER中具有这13个扩展中的任何一个的时间戳来拉动任何行。
有办法做到这一点吗?
我试过这样:
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);
但我根本没有记录
答案 0 :(得分:2)
我真的认为这通常会写成:
where date(ts) = '2017-10-16' and
(CALLINGPARTYNO in (7276, . . .) or -- I think the OP wants either one to match
FINALLYCALLEDPARTYNO in (7276, . . .)
);
如果每个应与扩展名匹配,请使用and
。如果需要匹配,请使用or
。
答案 1 :(得分:1)
我相当确定没有任何特定的语法来检查这样的“交叉点”,但这是最简单的。
... AND (callingpartyno IN (the list) OR finallycalledpartyno IN (the list))
旁注:使用WHERE条件中的几乎任何函数(如DATE()
)都会破坏查询的性能; ts >= '2017-10-16 00:00:00' AND ts < '2017-10-17 00:00:00'
通常是更好的选择。
答案 2 :(得分:1)
您可以使用子查询:
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)
;