我有这样的数据:
table1
id | part | price
1 | ox900 | 100
2 | ox980 | 200
和
table2
id | part | price
1 | ox560 | 560
2 | ox980 | 120
结果我想得到这样的架构:
id | part | priceTable1 | priceTable2 | minPrice
1 | ox900 | 100 | | 100
1 | ox980 | 200 | 120 | 120
1 | ox560 | | 560 | 560
简化它可以没有minPrice
列...
现在我有这样的疑问:
SELECT *
FROM (select part, price from supportContacts
union all
select part, price from supportContacts2)
group by part
但这并不是我想要实现的目标。
有可能以某种方式做,我上面已经描述过吗?
答案 0 :(得分:1)
SQLite不支持完全外部联接,因此首先获取所有部件的列表,然后使用左外部联接查找其价格:
SELECT part,
table1.price AS priceTable1,
table2.price AS priceTable2,
min(ifnull(table1.price, 'inf'),
ifnull(table2.price, 'inf')) AS minPrice
FROM (SELECT part FROM table1
UNION
SELECT part FROM table2)
LEFT JOIN table1 USING (part)
LEFT JOIN table2 USING (part);
(fiddle)