在MySQL中是否可以查看连接表中的字段是否在每一行中都没有相同的值。
我试图在this sqlfiddle
中将它放在一个简单的例子中-- Create tables
create table tbl_cake (
id INT
);
create table tbl_cake_piece (
id INT,
cake_id INT,
share INT
);
-- This cake is divided in 2 pieces with size 1/2
insert into tbl_cake values (1);
insert into tbl_cake_piece values (1, 1, 2);
insert into tbl_cake_piece values (2, 1, 2);
-- This cake is divided in 1 piece with size 1/2 and 2 pieces with size 1/4
insert into tbl_cake values (2);
insert into tbl_cake_piece values (3, 2, 2);
insert into tbl_cake_piece values (4, 2, 4);
insert into tbl_cake_piece values (5, 2, 4);
-- I want to select cakes that are not divided in equals pieces
-- So this query should return cake with id '2'
select * from tbl_cake c
join tbl_cake_piece p on p.cake_id = c.id
答案 0 :(得分:4)
未切成X片的蛋糕
select cake_id
from tbl_cake_piece
group by cake_id
having count(distinct share) > 1