在 WHERE 子句中为 IN 条件声明变量

Posted

技术标签:

【中文标题】在 WHERE 子句中为 IN 条件声明变量【英文标题】:Declare variable for IN condition in WHERE clause 【发布时间】:2020-02-06 08:05:42 【问题描述】:

我有下表,你也可以在SQL Fiddlehere找到:

CREATE TABLE Flows (
    Flow_Date DATE,
    Product TEXT,
    FlowType TEXT,
    Country TEXT,
    Quantity VARCHAR(255)
);

INSERT INTO Flows
(Flow_Date, Product, FlowType, Country, Quantity)
VALUES 
("2019-05-23", "Product A", "Inbound", "DE", "400"),
("2019-05-23", "Product A", "Inbound", "DE", "400"),
("2019-05-23", "Product B", "Inbound", "NL", "500"),
("2019-05-23", "Product B", "Inbound", "NL", "500"),
("2019-05-23", "Product A", "Outbound", "FR", "300"),
("2019-05-23", "Product A", "Outbound", "FR", "300"),
("2019-05-23", "Product B", "Outbound", "US", "200"),
("2019-05-23", "Product B", "Outbound", "US", "200"),
("2019-05-24", "Product A", "Inbound", "DE", "900"),
("2019-05-24", "Product A", "Inbound", "DE", "900"),
("2019-05-24", "Product B", "Inbound", "NL", "800"),
("2019-05-24", "Product B", "Inbound", "NL", "800"),
("2019-05-24", "Product A", "Outbound", "FR", "650"),
("2019-05-24", "Product A", "Outbound", "FR", "650"),
("2019-05-24", "Product B", "Outbound", "US", "450"),
("2019-05-24", "Product B", "Outbound", "US", "450"),
("2019-05-25", "Product A", "Inbound", "DE", "900"),
("2019-05-25", "Product A", "Inbound", "DE", "900"),
("2019-05-25", "Product B", "Inbound", "NL", "800"),
("2019-05-25", "Product B", "Inbound", "NL", "800"),
("2019-05-25", "Product A", "Outbound", "FR", "650"),
("2019-05-25", "Product A", "Outbound", "FR", "650"),
("2019-05-25", "Product B", "Outbound", "US", "450"),
("2019-05-25", "Product B", "Outbound", "US", "450"),
("2019-05-26", "Product A", "Inbound", "DE", "900"),
("2019-05-26", "Product A", "Inbound", "DE", "900"),
("2019-05-26", "Product B", "Inbound", "NL", "800"),
("2019-05-26", "Product B", "Inbound", "NL", "800"),
("2019-05-26", "Product A", "Outbound", "FR", "650"),
("2019-05-26", "Product A", "Outbound", "FR", "650"),
("2019-05-26", "Product B", "Outbound", "US", "450"),
("2019-05-26", "Product B", "Outbound", "US", "450");

我使用以下查询从该表中获取数据:

SELECT Flow_Date, Product, FlowType, Country
FROM Flows
WHERE Flow_Date BETWEEN ("2019-05-23 00:00:00") AND ("2019-05-25 23:59:59")
AND Country IN ("DE","NL")
GROUP BY 1,2,3,4;

到目前为止,这一切都完美无缺。


但是,现在我想在语句的WHERE 子句中为IN 条件设置一个变量。 因此,我尝试使用以下方法:

SET @country = ("DE", "NL");
SELECT Flow_Date, Product, FlowType, Country
FROM Flows
WHERE Flow_Date BETWEEN ("2019-05-23 00:00:00") AND ("2019-05-25 23:59:59")
AND Country IN @country
GROUP BY 1,2,3,4;

但是,我收到错误Operand should contain 1 column(s)

我需要对我的代码进行哪些更改才能使其正常工作?

【问题讨论】:

在这里查看解决方案:***.com/questions/4723100/… 我收到错误Operand should contain 1 column(s) 这是SET @country = ("DE", "NL"); 语句的错误。 我使用以下查询从该表中获取数据: GROUP BY by all output fieldlist?完全删除 GROUP BY 子句,但添加 DISTINCT。或者添加一些聚合操作,例如,SUM(Quantity)... 【参考方案1】:
SET @country = "DE,NL";

SELECT DISTINCT Flow_Date, Product, FlowType, Country
FROM Flows
WHERE Flow_Date BETWEEN ("2019-05-23 00:00:00") AND ("2019-05-25 00:00:00")
  AND FIND_IN_SET(Country, @country);

fiddle

【讨论】:

谢谢。正是我需要的。 sqlfiddle.com/#!9/e83241/39 @Michi 按所有输出字段分组会导致过度排序。请参阅我对此问题的评论。

以上是关于在 WHERE 子句中为 IN 条件声明变量的主要内容,如果未能解决你的问题,请参考以下文章

EF Core 在复杂的 Where 子句上声明多个变量

IN vs = in Where 子句

SQL Server - 带有声明变量的In子句[重复]

如何在 NOT IN 子句中使用字符串作为变量?

在SQL Server存储过程中声明变量列表

SQL Server If 条件在 where 子句?