c# - sql中如何比较三列

标签 c# sql sql-server

我想要比较表中 Col2 的 3 列值。

Col1 Col2 Col3

12 < 25 TRUE

25 > 20 TRUE

15 = 25 FALSE

SELECT (case when Col2 = '<' then  Col1 < Col3 else  Col1  end)

有人可以帮助我吗? 先进的感谢。

最佳答案

您可以尝试使用case,如下所示:

 case 
   when Col2 = '<' then
     Col1 < Col3
   when Col2 = '>' then
     Col1 > Col3
   when Col2 = '=' then
     Col1 = Col3
 end case

查询可能是

 select case 
          when col2 = '=' then
            case when (col1 = col3) then 1 else 0 end
          when col2 = '>' then
            case when (col1 > col3) then 1 else 0 end
          when col2 = '<' then
            case when (col1 < col3) then 1 else 0 end
          else
            0  
        end 
   from MyTable

另一种可能是

  (Col2 = '<' and (Col1 < Col3)) or
  (Col2 = '>' and (Col1 > Col3)) or
  (Col2 = '=' and (Col1 = Col3)) 

关于c# - sql中如何比较三列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36835409/

相关文章:

c# - 是否可以在 ASP.Net 属性中设置必需的属性?

sql-server - 将 SQL 数据库部署到 Azure 失败

sql - 如何找到 SQL Server 2008 中性能最差的查询?

c# - 如何确定应用程序的哪一部分正在泄漏内存?

c# - 禁用/启用表单时 ToolStripButton 仍然突出显示

c# - 我如何测试与 NUnit 的契约(Contract)?

SQL - 如何显示多行结果之间的差异

mysql - SQL select from inner join where count 大于

java - 用于投影的 Hibernate 缓存

sql - 多对多选择查询