我有一张桌子,列出了供应牛肉鸡肉的餐馆,接近这个:
Res Meat
res1 chicken
res1 chicken
res1 beef
res2 chicken
res2 chicken
res3 beef
我尝试编写一个查询,返回哪些餐馆只提供鸡肉或牛肉,但不是两者都没有消除重复结果。
答案 0 :(得分:1)
试试这个:
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
答案 1 :(得分:0)
尝试这种方式:
create table tab
(
Res varchar(10),
Meat varchar(10)
)
insert into tab ( Res, Meat ) values ( 'res1', 'chicken' )
insert into tab ( Res, Meat ) values ( 'res1', 'chicken' )
insert into tab ( Res, Meat ) values ( 'res1', 'beef' )
insert into tab ( Res, Meat ) values ( 'res2', 'chicken' )
insert into tab ( Res, Meat ) values ( 'res2', 'chicken' )
insert into tab ( Res, Meat ) values ( 'res3', 'beef' )
select Res
from (
select distinct Res,Meat
from tab ) T
group by Res
having count(1) = 1
答案 2 :(得分:0)
你可以使用except运算符
select * from table
where meat = 'chicken' or meat = 'Beer'
except
select * from table
where meat = 'chicken' and meat = 'Beer'
答案 3 :(得分:0)
尝试:
select Res
from rest
group by Res
having count(distinct Meat) = 1