比方說,我有以下型號:查詢在關聯表的外鍵
Customer(customer_id (PK), firstName, lastName, email)
Item(item_id (PK), name, description)
Orders(orders_id (PK), customer_id (FK), item_id (FK), promotion_id (FK)),
Promotion(promotion_id (PK), date, gift_id (FK))
Gift(gift_id (PK), name, description)
現在,讓我們說,我有以下要求:
檢索所有訂單列表(沒有分組)來自所有客戶以及來自物品和禮物的名稱列相關聯。
困難的部分是,關聯表的命令有一個外鍵列到一個到多個表(提升),輪到他有外鍵的禮物; 我有工作的下面的查詢,但我弄清楚,應該有一個更優雅的方式來解決這個問題不是做了很多的連接是這樣的:
select concat(c.firstName, ' ', c.lastName) as customerName,
i.name, g.name
from customer as c
left join orders as o on c.customer_id = o.customer_id
inner join item as i on o.item_id = i.item_id
inner join promotion as p on o.promotion_id = p.promotion_id
inner join gift as g on p.gift_id = g.gift_id;
我怎麼能解決一個更優雅的查詢辦法? 在此先感謝!
我寧願指定INNER JOIN,而不是剛剛加入,只是爲了確認這是你真正想要的東西。 –