在sql查询中需要帮助以根据以前的版本提供结果集

时间:2014-01-27 08:24:38

标签: sql sql-server sql-server-2008 sql-server-2005 sql-server-2008-r2

我有两个表,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”时才给我们结果集?

2 个答案:

答案 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))