gpt4 book ai didi

mysql - 选择连接表中的字段在每行中的值不相同的行

转载 作者:行者123 更新时间:2023-11-29 01:41:45 26 4
gpt4 key购买 nike

在 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

最佳答案

没有切成 X 等份的蛋糕

select cake_id
from tbl_cake_piece
group by cake_id
having count(distinct share) > 1

关于mysql - 选择连接表中的字段在每行中的值不相同的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19299369/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com