mysql - Querying a foreign key on a association table -
let's have following model:
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) now, let's have following requirement:
retrieve list of orders (not grouped by) customers , name column both item , gift associated.
the difficult part association table orders has foreign key column 1 many table (promotion) that, in turn, has foreign key gift; have following query worked, figure out should have more elegant way approach problem doing lot of joins this:
select concat(c.firstname, ' ', c.lastname) customername, i.name, g.name customer c left join orders o on c.customer_id = o.customer_id inner join item on o.item_id = i.item_id inner join promotion p on o.promotion_id = p.promotion_id inner join gift g on p.gift_id = g.gift_id; how resolve query in more elegant way? in advance!
i think elegant. joins classy , misunderstood.
Comments
Post a Comment