2

是否可以在 MySQL 中查看连接表中的字段在每一行中是否具有相同的值。

我试图把它放在这个 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
4

1 回答 1

4

未切成 X 等份的蛋糕

select cake_id
from tbl_cake_piece
group by cake_id
having count(distinct share) > 1
于 2013-10-10T15:12:01.013 回答