记录一下自己经常用的SQL语句
查询表中字段数据重复:
select * from table(表名) a where (a.字段) in (select 字段 from table(表名) group by 字段 having count(*) > 1)
判断字段是否为16进制:
select*from table(表名) A WHERE A.字段 LIKE '%[^0123456789ABCDEF]%' 这个可以找出小写的abcdef
判断字段是否为10进制:
select*from table(表名) A WHERE A.字段 LIKE '%[^0-9]%'
找出字段中为null的数据:
select*from table(表名) A WHERE A.字段 IS NULL
找出字段中为空的数据:
select*from table(表名) A WHERE A.字段 =''
两张表中相同数据和不同数据:
--1.select distinct A.ID from A where A.ID not in (select ID from B)
--2.select A.ID from A left join B on A.ID=B.ID where B.ID is null
--3.select * from B where (select count(1) as num from A where A.ID = B.ID) = 0
--4.SELECT * FROM b WHERE NOT EXISTS(SELECT 1 FROM a WHERE tel_no=b.tel_no)
--5.EXCEPT intersect
删除字段:
alter table table(表名)
drop column 字段
往一个表中插入另一个表的数据:
INSERT INTO table1(字段)
SELECT 字段 from table2
查询连续流水号中缺失的数据:
create table #tmp(缺失数据 nvarchar(10))
declare @i int
set @i = 4000001
while @i <= 4002500
begin
insert into #tmp select cast(@i as nvarchar)
set @i = @i + 1
end
select a.缺失数据
from #tmp a
left join table b on a.缺失数据=b.字段
where b.字段 is null
select a.字段
from table a
left join #tmp b on a.字段=b.缺失数据
where b.缺失数据 is null
drop table #tmp
这段SQL语句是创建一个临时表和原先的表比较来找出4000001- 4002500缺失的数据