【问题标题】:SQL - AND STATEMENT INSIDE RESULT CASE STATEMENTSQL - AND 语句在结果案例语句中
【发布时间】:2014-08-05 17:46:33
【问题描述】:

我想执行一些类似的事情

select * from table1
inner join table2 on table1.numero=table2.numero
 CASE WHEN table2.test = 'test'
THEN and  table2.test = table1.test END

我的意思是如果我有 table2.test = 'test' 那么 table2.test = table1.test

谢谢

【问题讨论】:

  • 你能试着澄清你在问什么吗?你是问如何在 SQL 中构造条件语句?
  • 所以当 table2.test = 'test' 时,你希望你的连接语句是 table1.numero = table2.numero 和 table2.test = table1.test?如果 table2.test 不等于 test,那不用担心?
  • 也许一些样本数据和结果会让你更清楚你想要达到的目标。

标签: sql select case inner-join conditional-statements


【解决方案1】:

试试这个……

SELECT *
FROM 
    table1
     INNER JOIN 
    table2 ON 
        table1.numero=table2.numero AND
        ((Table2.Test = 'test' AND Table1.test = Table2.test) OR Table2.Test != 'test')

【讨论】:

  • SELECT * FROM table1 INNER JOIN table2 ON table1.numero=table2.numero AND ((Table2.Test = 'test' AND Table1.test = Table2.test))
【解决方案2】:
select * from table1
inner join table2 on table1.numero=table2.numero
and CASE WHEN table2.test = 'test' then table2.test else 1 end
= CASE WHEN table2.test = 'test' then table1.test else 1 end

使用两个 case 语句,您可以构造一个 when table2.test = test then table2.test=table1.test else 1=1。我想这就是你想要的?

【讨论】:

  • select * from table1 inner join table2 on table1.numero=table2.numero and CASE WHEN table2.test = 'test' then table2.test else 1 end
【解决方案3】:

当我执行这个查询时

select * from table1
inner join table2 on table1.numero=table2.numero
and CASE WHEN table2.test = 'test' then table2.test else 1 end
= CASE WHEN table2.test = 'test' then table1.test else 1 end

我有一个类似的错误

“语法不正确 '='。”

对于第二个查询,为什么我们要放置一个语句 Or 我认为这是正确的:

SELECT *
FROM 
    table1
     INNER JOIN 
    table2 ON 
        table1.numero=table2.numero AND
        ((Table2.Test = 'test' AND Table1.test = Table2.test)) 

那你

【讨论】:

    猜你喜欢
    • 2016-04-18
    • 1970-01-01
    • 1970-01-01
    • 2018-08-10
    • 1970-01-01
    • 2014-06-19
    • 1970-01-01
    • 1970-01-01
    • 2015-01-18
    相关资源
    最近更新 更多