简体   繁体   中英

Checking for multiple values in multiple columns, sql select

I have a pretty SQL select query that works as follows:

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

This retrieves 24 records as it should. 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.

Let's say the previous query returns 24 and I do the same query with 7201 and it returns 6 rows. I want a way that would return 30 rows in that case. 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.

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. Use or if either one needs to match.

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; ts >= '2017-10-16 00:00:00' AND ts < '2017-10-17 00:00:00' is usually a much better choice.

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)
;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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