我想在一个条目中输出两行但有两个表。这是我的表:
TABLE_A
-----------------------------------------------------------------------
checkkey checknum confirmed printed canceled
-----------------------------------------------------------------------
1 1 Y Y Y
2 2 Y Y N
3 10 N Y Y
表-B
-----------------------------------------------------------------------
checkkey checknum status
-----------------------------------------------------------------------
1 1 V
2 2 V
3 10 V
我想要一个像这样的输出
-----------------------------------------------------------------------
checkkey checknum confirmed printed canceled status
-----------------------------------------------------------------------
1 1 Y Y Y
1 1 Y Y Y V
2 2 Y Y N
2 2 Y Y N V
3 10 N Y Y
3 10 N Y Y V
答案 0 :(得分:1)
您可以使用UNION,第一个查询将根据tableB获取状态为V的所有内容,第二个获取来自A的所有行
select a.checkkey, a.checknum,a.confirmed, a.printed,a.canceled, b.status
from table_A a
inner join table_B b
on a.checkkey = b.checkkey
union all
select a.checkkey, a.checknum,a.confirmed, a.printed,a.canceled, NULL as status
from table_A a
order by checkkey, checknum;
答案 1 :(得分:0)
我认为这可以满足您的需求:
select a.*, 'V' as status
from table_A a
union all
select a.*, NULL as status
from table_A a
order by checkkey, checknum;
Table_B
似乎没有必要。