mysql - Find all rows with matching columns with two conditions -
i have 2 tables , want find orders have two rows in line_items table , one of rows has sku of ball. trying find customers ordered ball, regardless of qty of ball row. there needs 2 rows per order @ least, because 1 of rows shipping sku.
in below data, john , sam valid orders returned because skus orders have in line_items table ball , shipping (regardless of ball qty). i'd tables joined returned data have order_id, customer, date_placed, , qty.
table orders id customer date_placed =========================== 0 john 1/1/2000 1 bill 2/1/2000 2 sam 2/5/2000 table line_items id order_id qty sku ========================= 0 0 1 ball 1 0 1 shipping 2 1 1 ball 3 1 1 rope 4 1 1 shipping 5 2 3 ball 6 2 1 shipping
thank much!
if understand correctly, should able write this:
select o.id, customer, date_placed, li.qty orders o inner join line_items li on o.id=li.order_id , li.sku='ball' exists( select order_id line_items tli o.id=tli.order_id group order_id having count(*)=2)
Comments
Post a Comment