你好,
所以我使用以下查询连接 5 个表:
order = db.session.query(Trailer, Trip, TripPallet, WaveOrder, Package)\
.join(Trip, Trailer.trailer_id == Trip.trailer_id)\
.join(TripPallet, Trip.trip_id == TripPallet.trip_id)\
.join(WaveOrder, TripPallet.pallet_id == WaveOrder.pallet_id)\
.join(Package, WaveOrder.order_id == Package.order_id)\
.filter(Trailer.trailer_id == '555555').all()
现在,5个表如下:
**Trailer Table**
trailer_id
555555
**Trip Table**
trip_id trailer_id
523462462 555555
**Trip Pallet table**
trip_id pallet_id
523462462 1052
523462462 1054
**Wave Order Table**
pallet_id order_id
1052 123456
1052 123457
1054 324567
1054 797453
**Package table**
order_id Tracking
123456 ABCKDF
123457 DVNIUO
324567 DNKLIN
797453 ADFNLN
因此,为了给您一个概览,预告片包含一次旅行。该行程包含托盘,每个托盘包含 wave,它们是一组包裹。
我在上面使用的查询将返回一个托盘上的所有包裹,但不会返回所有托盘。
当我通过 for 循环运行查询时,这是我的结果:
counter = 0
for trailer, trip, trippallet, wave_order, package in order:
counter += 1
print "%d The trailer id is %d and it contains the trip id %d with the pallet id %d and has the following order %d which " \
"has the following tracking number %s" % \
(counter, trailer.trailer_id, trip.trip_id, trippallet.pallet_id, wave_order.order_id, package.tracking_no)
1 The trailer id is 555555 and it contains the trip id 523462462 with the pallet id 1052 and has the following order 123456 which has the following tracking number ABCKDF
2 The trailer id is 555555 and it contains the trip id 523462462 with the pallet id 1052 and has the following order 123457 which has the following tracking number DVNIUO
如您所见,它仅从 trip pallet 表中获取第一个 pallet_id,即 1052。它还应该获取 pallet_id 1054 并返回该表中的所有包裹。
有谁知道如何让它返回 Trip Pallet 表中的第二个 pallet_id 及其所有对应的包裹?
最佳答案
你好,
问题不在于连接语句。连接语句是 100% 正确的。事实上,它是数据库。
DBA 删除了我完成连接语句所需的关键数据。
关于python - SQLAlchemy .all() 只返回多连接的第一个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31413102/