Does anyone know the solution to this problem ? There are 3 Tables: orders , order_groups and stores .
I want to list the orders, with the names of the stores where the order was placed, and where the order is going to be delivered.
I keep the from_store_id
, and to_store_id
in the order_groups
table
Listing these orders would be simple, i just left join the order_groups to orders , and select the name, from_shop_id
and to_shop_id
, but the problem is i want the name of the stores not the id, and the store names are placed in a different table (stores)
Here is what im talking about:
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
The statement i have yet:
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
Any idea how to select and join the store names to the query ?
One more question. I actually want to list two kind of orders in one query from different tables too, im on the right track with this structure ?
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
You only need to join 2 times the same table!
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.