[英]How to use subquery within a case statement?
I have two tables . 我有两张桌子。
table1 表格1
t1id var1 var2
1 xxx ccc
2 ccc ddd
3 eee bbb
table2 表2
t2id attribute
1 lll
3 ggg
4 ggg
5 lll
I need to select * from table1, and concatenate t1id
with attribute
if t1id=t2id
我需要从table1中选择*,并且如果
t1id=t2id
将t1id
与attribute
连接
Here is what I tried 这是我尝试过的
SEL CASE WHEN (t1id IN (SELECT t2id FROM table2) )
THEN t1id || attribute
ELSE t1id END AS t1idModified,
var1 , var2
FROM table1,table2
However, it did not work . 但是,它没有用。 How can I use a subquery within case?
如何在大小写范围内使用子查询?
Even if this syntax was allowed it would not return the expected result. 即使允许使用此语法,也不会返回预期结果。
What's the datatype of t1id
, numeric or string? t1id
,数字或字符串的数据类型是什么?
You can rewrite it as an Outer Join (return a row even if there's no match) and a COALESCE (concat the attribute or ''): 您可以将其重写为外部联接(即使没有匹配项,也返回一行)和COALESCE(连接属性或”):
SELECT
TRIM(t1.t1id) || (COALESCE(t2.ATTRIBUTE, '')) AS t1idModified,
var1, var2
FROM table1 AS t1
LEFT JOIN table2 AS t2
ON t1.t1id = t2.t2id
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.