CASE语句切换WHERE条件

发布于 2024-09-09 23:51:53 字数 247 浏览 6 评论 0原文

我希望有一个如下所示的 t-sql 语句...

SELECT [field]
FROM [table]
WHERE CASE @flag
        WHEN 1 THEN col1 = @value
        ELSE col2 = @different_value
      END

重点是我想在我的函数中放置一个标志以在同一查询中使用不同的 where 子句。我就是无法让它发挥作用。这可能吗?

I would love to have a t-sql statement like the following...

SELECT [field]
FROM [table]
WHERE CASE @flag
        WHEN 1 THEN col1 = @value
        ELSE col2 = @different_value
      END

The point being I want to put a flag in my function to use different where clauses in the same query. I just can't get it to work. Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

听闻余生 2024-09-16 23:51:53

这对你有用吗?

Where (@flag = 1 and col1 = @value) or (@flag != 1 and col2 = @different_value). 

Will this work for you?

Where (@flag = 1 and col1 = @value) or (@flag != 1 and col2 = @different_value). 
梦行七里 2024-09-16 23:51:53

使用 mySQL,您的语句应该可以工作,因为 mySQL 支持二进制表达式。

所以你必须尝试这个,

SELECT [field] 
FROM [table] 
WHERE CASE @flag 
        WHEN 1 
        THEN case when col1 = @value then 1 else 0 end
        ELSE case when col2 = @different_value then 1 else 0 end 
      END = 1

这不是很好的可读性。请注意性能问题,因为优化器可能会在这里遇到困难。

With mySQL your statement should work, because mySQL supports binary expressions.

So you have to try this

SELECT [field] 
FROM [table] 
WHERE CASE @flag 
        WHEN 1 
        THEN case when col1 = @value then 1 else 0 end
        ELSE case when col2 = @different_value then 1 else 0 end 
      END = 1

That isn't pretty good readable. Please be aware of performance issues, because the optimizer may struggle here.

五里雾 2024-09-16 23:51:53

根据给定参数动态改变搜索是一个复杂的主题,并且以一种不同的方式进行,即使只有非常微小的差异,也会产生巨大的性能影响。关键是使用索引,忽略紧凑的代码,忽略担心重复代码,必须制定好的查询执行计划(使用索引)。

阅读本文并考虑所有方法。您的最佳方法将取决于您的参数、数据、模式和实际使用情况:

动态搜索《T-SQL 条件》,作者:Erland Sommarskog

动态 SQL 的诅咒与祝福,作者:Erland Sommarskog< /a>

这将产生最佳执行计划:

IF @flag=1
BEGIN
    SELECT
        [field]
        FROM [table]
        WHERE col1 = @value
END
ELSE
BEGIN
    SELECT
        [field]
        FROM [table]
        WHERE col2 = @different_value
END

但是,如果您必须有一个查询,那么这是使用索引的最佳选择

SELECT
    [field]
    FROM [table]
    WHERE @flag=1
        AND col1 = @value
UNION ALL
SELECT
    [field]
    FROM [table]
    WHERE @flag!=1
        AND col2 = @different_value

Dynamically changing searches based on the given parameters is a complicated subject and doing it one way over another, even with only a very slight difference, can have massive performance implications. The key is to use an index, ignore compact code, ignore worrying about repeating code, you must make a good query execution plan (use an index).

Read this and consider all the methods. Your best method will depend on your parameters, your data, your schema, and your actual usage:

Dynamic Search Conditions in T-SQL by by Erland Sommarskog

The Curse and Blessings of Dynamic SQL by Erland Sommarskog

this will produce the best execution plan:

IF @flag=1
BEGIN
    SELECT
        [field]
        FROM [table]
        WHERE col1 = @value
END
ELSE
BEGIN
    SELECT
        [field]
        FROM [table]
        WHERE col2 = @different_value
END

However, if you have to have a single query, this is your best bet at using an index

SELECT
    [field]
    FROM [table]
    WHERE @flag=1
        AND col1 = @value
UNION ALL
SELECT
    [field]
    FROM [table]
    WHERE @flag!=1
        AND col2 = @different_value
﹉夏雨初晴づ 2024-09-16 23:51:53

您还可以使用布尔逻辑:

SELECT blah FROM myTable WHERE (@i IS NULL OR AnotherCondition)

You could also use boolean logic:

SELECT blah FROM myTable WHERE (@i IS NULL OR AnotherCondition)
灯下孤影 2024-09-16 23:51:53

简单点怎么样?

WHERE
  (@flag = 1 AND col1 = @value)
OR
  (@flag = 2 AND col2 = @different_value)
...

How about simply;

WHERE
  (@flag = 1 AND col1 = @value)
OR
  (@flag = 2 AND col2 = @different_value)
...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文