sql - Where clause with multiple values -
i new sql , using ms sql server management studio 2014. have 3 tables called pizza, pizza_topping , topping. want list pizzas have topping1 , topping2 toppings. have came with,
select pizza.pizzaid, pizza.pizzaname, topping.toppingname pizza left join pizza_topping on pizza.pizzaid = pizza_topping.pizzaid left join topping on topping.toppingid = pizza_topping.toppingid topping.toppingname in ('topping1', 'topping2')
and gives me
pizzaid pizzaname toppingname -------- ---------------- -------------- pz002 | pizza1 | topping1 pz002 | pizza1 | topping2 pz010 | pizza5 | topping1 pz010 | pizza5 | topping2 pz011 | pizza6 | topping1 pz012 | pizza7 | topping2
i need first 4 rows because last 2 pizzas have 1 of toppings not both. have tried well,
group pizza.pizzaid,pizza.pizzaname, topping.toppingname having count(toppingname) >= 2
but didn't give expected result. line can used if going display pizzaid
, pizzaname
want display toppingname
well.
to more clear, expecting
pizzaid pizzaname toppingname -------- ---------------- -------------- pz002 | pizza1 | topping1 pz002 | pizza1 | topping2 pz010 | pizza5 | topping1 pz010 | pizza5 | topping2
please tell me how achieve result. thanks
i think done more if can count()
in subquery , join pizzaid's have count greater 1 this:
select q1.pizzaid ,q1.pizzaname ,t.toppingname ( select pizza.pizzaid ,pizza.pizzaname ,count(topping.toppingname) total_count pizza inner join pizza_topping on pizza.pizzaid = pizza_topping.pizzaid inner join topping on topping.toppingid = pizza_topping.toppingid group pizza.pizzaid ,pizza.pizzaname having count(topping.toppingname) > 1 ) q1 inner join pizza_topping pt on q1.pizzaid = pt.pizzaid inner join topping t on t.toppingid = pt.toppingid t.toppingname in ('topping1', 'topping2')
Comments
Post a Comment