簡體   English   中英

帶有兩個單獨聯接的MySQL查詢

[英]Mysql Query with two seperate join

有誰知道解決這個問題的辦法? 有3個表: ordersorder_groupsstores
我想列出訂單,以及下訂單的商店名稱以及要下達訂單的商店的名稱。

我將from_store_idto_store_idorder_groups表中

列出這些訂單很簡單,我只剩下將order_groups加入orders ,然后選擇名稱from_shop_idto_shop_id ,但是問題是我想要商店的名稱而不是ID,並且商店名稱被放置在另一個表中(店)

我在說什么:

Table orders
id  group_id    name            madeup_id
1     11        johnny cash         1
2     12        billy bob           1
LEFT JOIN order_groups on order_groups.id = orders.group_id
Table order_groups
id   from_store_id   to_store_id
11     55               56
12     56               55
Table stores
id    store_name
55     thisstore
56     thatstore
The result im looking for is:
   name         from_store   to_store
1.johhny cash   thisstore, thatstore
2.billy bob     thatstore, thisstore

我尚未發表的聲明:

SELECT 
orders.name, something as from_store, something as to_store
FROM orders
LEFT JOIN order_groups on order_groups.id = orders.group_id

somehow join stores on the order_groups.from_store_id = stores.id

WHERE orders.madeup_id = 1

知道如何選擇商店名稱並將其加入查詢嗎?

還有一個問題。 我實際上也想在一個查詢中列出來自不同表的兩種訂單,即時消息在這種結構的正確軌道上?

SELECT a,b FROM a LEFT JOIN b ON b.something=a.something WHERE something 
UNION ALL
SELECT a,b FROM c LEFT JOIN c ON c.something=a.something WHERE something

您只需要加入2次同一張桌子!

SELECT 
orders.name, fromStore.store_name as from_store, toStore.store_name as to_store
FROM orders
LEFT JOIN order_groups on order_groups.id = orders.group_id
left join stores fromStore on the order_groups.from_store_id = fromStore.id
left join stores toStore on the order_groups.to_store_id = toStore.id
WHERE orders.madeup_id = 1

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM