SQL - 和声明结果案例声明

时间:2014-08-05 17:46:34

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

我想执行类似的事情

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

感谢

3 个答案:

答案 0 :(得分:0)

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然后table2.test = table1.test else 1 = 1。我想那就是你想要的?

答案 1 :(得分:0)

试试这个......

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

答案 2 :(得分:0)

执行此查询时

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

我有一个像

这样的错误

“Syntaxe incorrecte vers'='。”

对于第二个查询,我们为什么要发表声明或  我认为这是正确的:

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

然后你