2

SQL的极端新手在这里。有没有更简单的方法来做到这一点?

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;

希望 SQL 有类似的数组列表:

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

2 回答 2

9

您可以使用以下IN关键字:

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;
于 2013-05-16T00:11:24.180 回答
1

虽然这个IN词在这里可能更合适,但您也可以使用表变量并加入它:

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;

我实际上还没有测试过错别字,但我认为它有效。就像我之前说的,IN在这个例子中关键字可能更适合你

于 2013-05-16T00:51:07.507 回答