如何使IN运算符工作以使硬编码值匹配

时间:2014-06-06 17:03:19

标签: sql postgresql in-clause

Have accountId ={001,3456,78902,456}

我想使用accountId子句和IN操作从现有表中获取这些left join on的详细信息,我应该如何形成查询?作为DB end的新手,我无法将正确的部分组合在一起:

select t1.col2, case when CASE WHEN t1.col3 is null THEN 'False' ELSE 'True' END as value
FROM table2 t2 left join table1 t1 on t2.col1=t1.col1
WHERE accountID IN (001,3456,78902,456); //The query is wrong because, I wanted to map using those accountID and get the data?

任何有关形成查询的快速帮助都很棒

现有表格中有accountId作为其中一列。

1 个答案:

答案 0 :(得分:0)

猜猜......

SELECT t2.col1, t1.col2, (t1.col3 IS NULL) AS value
FROM   table2 t2
LEFT   JOIN table1 t1 USING (col1)
WHERE  t2.accountid IN (1,3456,78902,456); -- t1 or t2 ???

我添加了t2.col1,或者您可能会获得只有NULL个值的行 使用更简单的(t1.col3 IS NULL) AS value直接返回boolean TRUE / FALSE
还可以使用更简单的USING来连接同名的列。

如果{1}}也在table1中,则根本不需要加入..

accountid