由于您没有在问题中说明您使用的是什么数据库,因此我建议您进行一个适用于所有数据库平台的查询。但是此查询要求您创建一个具有 auto_number、identity、serial 等属性的新列
这将是查询:
select * from tbl
where (id,auto_number_here) in
(select id, min(auto_number_here)
from tbl
group by id)
这将适用于除 Sql Server 之外的许多平台。Sql Server 不支持元组。你必须这样做:
select * from tbl x
where
-- leave this comment, so it mimics the tuple capability
-- (id,auto_number_here) in
EXISTS
(select
-- Replace this:
-- id, min(auto_number_here)
-- With whatever floats your boat,
-- you can use 1, null(the value generated by Entity Framework's EXIST clause),
-- even 1/0 is ok :-) (this will not result to divide-by-zero error)
-- But I prefer retaining the columns, so it mimics the tuple-capable database:
id, min(auto_number_here)
from tbl
where id = x.id
group by id
having x.auto_number_here = min(auto_number_here))
元组相关问题:在 sql in 子句中使用元组
由于某些数据库不支持元组,您可以改为模拟它
select z.* from tbl z
join (select id, min(auto_number_here) as first_row from tbl group by id) as x
on z.id = x.id and z.auto_number_here = x.first_row
它比 EXISTS 方法好一点。但是,如果您的数据库支持元组,请改用它;尽可能只使用JOIN来反映表关系,使用WHERE子句进行过滤。
更新
也许一个具体的例子可以清楚地解释它,假设我们有一个我们忘记在其上放置主键的现有表:
create table tbl(
id varchar(5), -- supposedly primary key
data int,
message varchar(100)
);
insert into tbl values
('A',1,'the'),
('A',1,'quick'),
('A',4,'brown'),
('B',2, 'fox'),
('B',5, 'jumps'),
('B',5, 'over'),
('C',6, 'the'),
('C',7, 'lazy');
为了从重复中只提取一行,我们需要在现有数据上添加第三列。
这将帮助我们从重复项中提取一行且仅一行
alter table tbl add auto_number_here int identity(1,1) not null;
现在应该可以工作了:
select z.* from tbl z
join (select id, min(auto_number_here) as first_row from tbl group by id) as x
on z.id = x.id and z.auto_number_here = x.first_row
现场测试:http ://www.sqlfiddle.com/#!6/19b55/3
这是这样的:
select * from tbl x
where
-- leave this comment, so it mimics the tuple capability
-- (id,auto_number_here) in
EXISTS
(
select
-- Replace this:
-- id, min(auto_number_here)
-- With whatever floats your boat,
-- you can use 1, null(the value generated by Entity Framework's EXIST clause),
-- even 1/0 is ok :-) (this will not result to divide-by-zero error)
-- But I prefer retaining the columns, so it mimics the tuple-capable database:
id, min(auto_number_here)
from tbl
where id = x.id
group by id
having x.auto_number_here = min(auto_number_here)
)
现场测试:http ://www.sqlfiddle.com/#!6/19b55/4