SQL 查询表中某一字段不重复的所有数据

SQL 查询表中某一字段不重复的所有数据,第1张

CREATE
TABLE
#A
(
id
INT,
name
varchar(4),
number
INT
);
INSERT
INTO
#A
SELECT
1,
'a',
123
UNION
ALL
SELECT
2,
'b',
152
UNION
ALL
SELECT
3,
'c',
123
UNION
ALL
SELECT
4,
'd',
520
UNION
ALL
SELECT
5,
'e',
300;
GO
SELECT

FROM
#A
main
WHERE
NOT
EXISTS
(
SELECT
1
FROM
#A
sub
WHERE
mainid
<>
subid
AND
mainnumber
=
subnumber
);
GO
id
name
number
-----------
----
-----------
2
b
152
4
d
520
5
e
300
(3
行受影响)

可以参考使用如下写法\x0d\如果要全部数据则可以\x0d\\x0d\select from table T1 where 字段 = (select max(字段) from table T2 where T1字段2 = T2字段2)\x0d\或\x0d\select from table T1 where not exists(select from table T2 where T1字段2 = T2字段2 and T1字段 回答于 2022-11-16

1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断

select from peoplewhere peopleId in (select   peopleId from   people group by   peopleId having count (peopleId) > 1)

2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录

delete from people where peopleId in (select   peopleId from people group by   peopleId   having count (peopleId) > 1)and rowid not in (select min(rowid) from   people group by peopleId having count(peopleId )>1)

3、查找表中多余的重复记录(多个字段)

select from vitae awhere (apeopleId,aseq) in   (select peopleId,seq from vitae group by peopleId,seq having

扩展资料

FROM子句指定SELECT语句查询及与查询相关的表或视图。在FROM子句中最多可指定256个表或视图,它们之间用逗号分隔。

在FROM子句同时指定多个表或视图时,如果选择列表中存在同名列,这时应使用对象名限定这些列所属的表或视图。

例如在usertable和citytable表中同时存在cityid列,在查询两个表中的cityid时应使用下面语句格式加以限定:

SELECTusername,citytablecityid

FROMusertable,citytable

WHEREusertablecityid=citytablecityid

在FROM子句中可用以下两种格式为表或视图指定别名

表名 as 别名

表名 别名

参考资料:

百度百科 SELECT语句

((select a from tableA ) except (select a from tableB))
union all
((select a from tableB ) except (select a from tableA))

select top 4 UserName,sum(Cost) as Cost
from Buy
where InFo='已付款'
and Adtime <= DATEADD(n,15,'2010-5-20 12:20:00')
group by UserName
order by 2 desc

--表变量
declare @tmp table(id int default(0), deptID int)
insert into @tmp(deptid)select distinct deptID from doc
--随机更新一个对应的doc表id上去
update a set aid=bid from @tmp as a,doc as b where adeptID=bdeptID

select a,b from dept as a,doc as b,@tmp as c where aid=cdeptID and bid=cid


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/13396414.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-28
下一篇 2023-07-28

发表评论

登录后才能评论

评论列表(0条)

保存