MySQL Union and JOIN -
i have following tables
purchase_order item ------------------ ------------ id (pk) id (pk) deleted deleted name name purchase_order_id (fk)
how return list of items linked non-deleted, non-null purchase_order, , purchase_orders have no items linked them, either deleted or not.
e.g example tables
purchase_order
id name deleted --------------------------- 1 big sale 0 2 other sale 1 3 empty sale 0
item
id name deleted purchase_order_id ---------------------------------------------- 1 fruit 1 1 2 bread 0 1 3 water 0 2
the correct query gives me this:
po_id name item_id name ------------------------------------ 1 big sale 2 bread 3 other sale null null
edit: i've got problem stipulating i.deleted = 0, stops rows don't join on item returning
select po.id, po.name, i.id, i.name purchase_order po left join item on i.purchase_order_id=po.id po.deleted = 0 , i.deleted = 0
@strawberry gave me answer
select po.id, po.name, i.id, i.name purchase_order po left join item on i.purchase_order_id=po.id , i.deleted = 0 po.deleted = 0
Comments
Post a Comment