我如何加入一个 sql table 并第一次出现在另一个 table 中,保持所有从第一次出现 table

How do I join all in one sql table and with first occurence from another table, keeping all from first table

以下 SQLite(android 房间)查询:

(更新 2021.01.03 07:43 GMT:我增强了 Q)

select  o.*, gi.* 
from orders as o
left join galleryitems as gi on gi.siteId = o.siteId 
order by orderId asc

使用这些给定的表格:

orders: unique key is orderid

o.orderId | siteId      | col1      | col2
----------+-------------+-----------+----------
456       | 1001        | o.456c1r1 | o.456c2r1 
457       | 1002        | o.457c1r1 | o.457c2r1
458       | 1003        | o.458c1r1 | o.458c2r1
459       | 1004        | o.459c1r1 | o.459c2r1
460       | 1005        | o.460c1r1 | o.460c2r1
461       | 1006        | o.461c1r1 | o.461c2r1
462       | 1007        | o.462c1r1 | o.462c2r1


galleryitems: unique key is galleryId, reference to ordertable is gi.siteId

gi.galleryID | gi.col1     | gi.col2     | gi.siteId 
-------------+-------------+-------------+----------
0001         | gi1001.c1r1 | gi1001.c2r1 | 1001      
0002         | gi1001.c1r2 | gi1001.c2r2 | 1001      
0003         | gi1002.c1r1 | gi1002.c1r1 | 1002
0004         | gi1003.c1r1 | gi1003.c1r1 | 1005
0005         | gi1003.c1r2 | gi1003.c1r2 | 1005

给出这个结果:


o.orderId | o.siteId | col1      | col2      | gi.galleryID | gi.col1     | gi.col2     | gi.siteId 
----------+----------+-----------+-----------+--------------+-------------+-------------+----------
456       | 1001     | o.456c1r1 | o.456c2r1 | 0001         | gi1001.c1r1 | gi1001.c2r1 | 1001   <-- "duplicates"   
456       | 1001     | o.456c1r1 | o.456c2r1 | 0002         | gi1001.c1r2 | gi1001.c2r2 | 1001   <-- "duplicates"
457       | 1002     | o.457c1r1 | o.457c2r1 | 0003         | gi1002.c1r1 | gi1002.c1r1 | 1002
458       | 1003     | o.458c1r1 | o.458c2r1 | <null>       | <null>      | <null>      | <null>
459       | 1004     | o.459c1r1 | o.459c2r1 | <null>       | <null>      | <null>      | <null>
460       | 1005     | o.460c1r1 | o.460c2r1 | 0004         | gi1003.c1r1 | gi1003.c1r1 | 1005   <-- "duplicates"
460       | 1005     | o.460c1r1 | o.460c2r1 | 0005         | gi1003.c1r2 | gi1003.c1r2 | 1005   <-- "duplicates"
461       | 1006     | o.461c1r1 | o.461c2r1 | <null>       | <null>      | <null>      | <null>
462       | 1007     | o.462c1r1 | o.462c2r1 | <null>       | <null>      | <null>      | <null>

正如您在下面看到的,我希望只有在 gi 出现时才会出现数据,但只有第一次出现时才会出现,所以它们不会“重复”(如果 [=39 只需填写客栈数据=] 被发现(第一次出现是可以的,或者甚至被计算在内,如果在加入期间可能的话),否则 gi 字段应该是 <null> ):


o.orderId | o.siteId | col1      | col2      | gi.galleryID | gi.col1     | gi.col2     | gi.siteId 
----------+----------+-----------+-----------+--------------+-------------+-------------+----------
456       | 1001     | o.456c1r1 | o.456c2r1 | 0001         | gi1001.c1r1 | gi1001.c2r1 | 1001   <-- no "duplicates"   
457       | 1002     | o.457c1r1 | o.457c2r1 | 0003         | gi1002.c1r1 | gi1002.c1r1 | 1002
458       | 1003     | o.458c1r1 | o.458c2r1 | <null>       | <null>      | <null>      | <null>
459       | 1004     | o.459c1r1 | o.459c2r1 | <null>       | <null>      | <null>      | <null>
460       | 1005     | o.460c1r1 | o.460c2r1 | 0004         | gi1003.c1r1 | gi1003.c1r1 | 1005   <-- no "duplicates"
461       | 1006     | o.461c1r1 | o.461c2r1 | <null>       | <null>      | <null>      | <null>
462       | 1007     | o.462c1r1 | o.462c2r1 | <null>       | <null>      | <null>      | <null>

我看到有很多建议,但它们无法应对 SQLite 有限的库。

有什么建议吗?

使用 androidx 房间。

您可以通过聚合来获得最小值为 galleryitems.row1 的行,方法是使用 SQLite 的一个非标准 SQL 但它是documented:

select  min(gi.row1) as row1, gi.row2, gi.siteId, o.* 
from orders as o left join galleryitems as gi 
on gi.siteId = o.siteId 
group by o.siteId
order by orderId asc 

或使用galleryitemsrowid来定义第一次出现:

select  gi.*, o.* 
from orders as o left join galleryitems as gi 
on gi.siteId = o.siteId 
group by o.siteId
having min(gi.rowid) or min(gi.rowid) is null
order by orderId asc

参见demo

@forpas 将我纠正到宽阔的道路上,我什至可以加强它。谢谢!

select count(gi.siteId) as cc, gi.*, o.*
from orders as o
left join galleryitems as gi on gi.siteId = o.siteId
group by o.siteId 

这个结果现在变成了这个结果:(注意 !! group by o.siteId 而不是 gi.siteId)

cc |o.orderId | o.siteId | col1      | col2      | gi.galleryID | gi.col1     | gi.col2     | gi.siteId 
---+----------+----------+-----------+-----------+--------------+-------------+-------------+----------
2  |456       | 1001     | o.456c1r1 | o.456c2r1 | 0001         | gi1001.c1r1 | gi1001.c2r1 | 1001   <-- no "duplicates"   
1  |457       | 1002     | o.457c1r1 | o.457c2r1 | 0003         | gi1002.c1r1 | gi1002.c1r1 | 1002
0  |458       | 1003     | o.458c1r1 | o.458c2r1 | <null>       | <null>      | <null>      | <null>
0  |459       | 1004     | o.459c1r1 | o.459c2r1 | <null>       | <null>      | <null>      | <null>
2  |460       | 1005     | o.460c1r1 | o.460c2r1 | 0004         | gi1003.c1r1 | gi1003.c1r1 | 1005   <-- no "duplicates"
0  |461       | 1006     | o.461c1r1 | o.461c2r1 | <null>       | <null>      | <null>      | <null>
0  |462       | 1007     | o.462c1r1 | o.462c2r1 | <null>       | <null>      | <null>      | <null>