user1899415
user1899415

Reputation: 3125

SQL simple AND alternative?

extreme newbie at SQL here. Is there a simpler way to do this?

SELECT event_type, flow, userid
FROM checkpoints_esc 
WHERE date='2013-05-14' AND event_type='flow_started' AND flow='1921474754' 
  OR flow='3326882819' OR flow='1916289507' OR flow='2121958995' 
  OR flow='2142167604'
LIMIT 1000;

Was hoping SQL has something array list like:

MyFlows = @[1921474754, 3326882819, 1916289507, 2121958995, 2142167604]
WHERE date='2013-05-14' AND event_type='flow_started' AND @MyFlows

Upvotes: 2

Views: 65

Answers (2)

Greg
Greg

Reputation: 3522

Although the IN word would probably be more appropriate here, you could also use a table variable and join to it:

DECLARE @T1 Table
(
  Flow VARCHAR(50)
)

INSERT INTO @T1 VALUES ('1921474754')
INSERT INTO @T1 VALUES ('3326882819')
...
INSERT INTO @T1 VALUES ('2142167604')

SELECT event_type, flow, userid
FROM checkpoints_esc
INNER JOIN @T1 T1 on T1.Flow = checkpoints_esc.Flow
WHERE date='2013-05-14' AND event_type='flow_started'

LIMIT 1000;

I haven't actually tested this for typos, but I think it works. Like I said earlier though, the IN keyword will probably suit you better in this example

Upvotes: 1

Justin Helgerson
Justin Helgerson

Reputation: 25521

You can use the IN keyword:

SELECT event_type, flow, userid
FROM checkpoints_esc 
WHERE date='2013-05-14' AND event_type='flow_started'
AND flow IN ('1921474754', '3326882819', '1916289507', '2121958995', '2142167604')
LIMIT 1000;

Upvotes: 9

Related Questions