我有两个表,table1和table2。
table1 id name city uqid 1 vikas mysore 2 table2 id uqid name status 1 1 vikas pending 1 2 Vikas processing
我有一个SQL查询来获取与table2
连接的table1的详细信息select table1.id,
table1.name,
table1.city,
table2.status
from table1
left outer join table2
on table2.uqid = table1.uqid
and table2.id = table1.id
这会给我结果集
id name city status 1 vikas mysore processing
我怎样才能修改上面的查询,直到在table2中为uqid = 1和id = 1将状态设置为“pass”时才给我们结果集?
答案 0 :(得分:0)
请尝试以下操作。
select table1.id,
table1.name,
table1.city,
table2.status
from table1
left outer join table2
on table2.uqid = table1.uqid
and table2.id = table1.id
where table2.status ilike 'pass';
如果声明你需要table2的uqid = id = 1,那么你的意思是你需要两个字段具有相同的值,然后使用以下内容。
select table1.id,
table1.name,
table1.city,
table2.status
from table1
left outer join table2
on table2.uqid = table1.uqid
and table2.id = table1.id
where table2.status ilike 'pass' and table2.uqid=table2.id;
建议:尝试规范化表格
答案 1 :(得分:0)
我不确定这是正确的方式还是存在任何其他有效方式,但这会给你想要的结果。
select table1.id,
table1.name,
table1.city,
table2.status
from table1
left outer join table2
on table2.uqid = table1.uqid and table2.id = table1.id
where table1.id in(select distinct id from table2 where status like 'pass'
and uqid not in(select uqid from table1))