I have a table that lists restaurants that serve either chicken of beef, something close to this:
Res Meat
res1 chicken
res1 chicken
res1 beef
res2 chicken
res2 chicken
res3 beef
and I'm trying to write a query that returns which restaurants only provide chicken OR beef but not both without eliminating duplicates from the result.
Try this:
CREATE TABLE rest
(
id int auto_increment primary key,
Res varchar(20),
Meat varchar(20)
);
INSERT INTO rest
(Res,Meat)
VALUES
('res1','chicken'),
('res1', 'chicken'),
('res1', 'beef'),
("res2", 'chicken'),
("res2", 'chicken'),
( 'res3', 'beef' )
select Res from
(select distinct Res,Meat from rest ) test
group by Res
having count(1) = 1
Example on SQLFiddle,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With