insert into city
--找出Hotel表中不存在于city表中的城市名
select DISTINCT rtrim(ltrim(cityname)) from Hotel e WHERE not EXISTS
(SELECT * FROM city eb WHERE eb.cityname=e.cityname)
--------------------------------Insert---------------------------------
-- 多条数据一次insert插入
INSERT INTO table1
SELECT '张三1','aaa','90' union all
SELECT '张三2','aaa','90' union all
SELECT '张三3','aaa','90'
/*
INSERT INTO SELECT语句
语句形式为:Insert into Table2(field1,field2,...) select value1,value2,... from Table1
要求目标表Table2必须存在,由于目标表Table2已经存在,所以我们除了插入源表Table1的字段外,还可以插入常量。示例如下:
INSERT INTO SELECT语句复制表数据
*/
--1.创建测试表
create TABLE Table1
(
a varchar(10),
b varchar(10),
c varchar(10),
CONSTRAINT [PK_Table1] PRIMARY KEY CLUSTERED
(
a ASC
)
) ON [PRIMARY]
create TABLE Table2
(
a varchar(10),
c varchar(10),
d int,
CONSTRAINT [PK_Table2] PRIMARY KEY CLUSTERED
(
a ASC
)
) ON [PRIMARY]
GO
--2.创建测试数据
Insert into Table1 values('赵','asds','90')
Insert into Table1 values('钱','asds','100')
Insert into Table1 values('孙','asds','80')
Insert into Table1 values('李','asds',null)
GO
select * from Table2
--3.INSERT INTO SELECT语句复制表数据
Insert into Table2(a, c, d) select a,c,5 from Table1
GO
--4.显示更新后的结果
select * from Table2
GO
/* 结果
a c d
李 NULL 5
钱 100 5
孙 80 5
赵 90 5
*/
--5.删除测试表
drop TABLE Table1
drop TABLE Table2
/*
SELECT INTO FROM语句
语句形式为:SELECT vale1, value2 into Table2 from Table1
要求目标表Table2不存在,因为在插入时会自动创建表Table2,并将Table1中指定字段数据复制到Table2中。示例如下:
SELECT INTO FROM创建表并复制表数据
*/
--1.创建测试表
create TABLE Table1
(
a varchar(10),
b varchar(10),
c varchar(10),
CONSTRAINT [PK_Table1] PRIMARY KEY CLUSTERED
(
a ASC
)
) ON [PRIMARY]
GO
--2.创建测试数据
Insert into Table1 values('赵','asds','90')
Insert into Table1 values('钱','asds','100')
Insert into Table1 values('孙','asds','80')
Insert into Table1 values('李','asds',null)
GO
--3.SELECT INTO FROM语句创建表Table2并复制数据
select a,c INTO Table2 from Table1
GO
--4.显示更新后的结果
select * from Table2
GO
/*
a c
李 NULL
钱 100
孙 80
赵 90
*/
--5.删除测试表
drop TABLE Table1
drop TABLE Table2
-- 根据表Adjustment中的记录删除Emp_Bak中对应的数据
DELETE Emp_Bak E
WHERE EXISTS
(
SELECT 'X'
FROM Adjustment A
WHERE E.Emp_Id = A.Emp_Id
)
DELETE Emp_Bak E
WHERE Emp_Id IN
(
SELECT A.Emp_Id
FROM Adjustment A
WHERE E.Emp_Id = A.Emp_Id
)
--103, ORACLE 中删除表Emp_Bak中重复数据
--JOIN
DELETE Emp_Bak D
WHERE ROWID >
(
SELECT MIN(ROWID)
FROM Emp_Bak R
WHERE D.Emp_Id = R.Emp_Id
)
--NOT IN
DELETE Emp_Bak D
WHERE ROWID NOT IN
(
SELECT MIN(ROWID)
FROM Emp_Bak R
GROUP BY R.Emp_Id
)
--104, SQL SERVER 删除重复数据 分三步 首先将非重复数据存储到临时表,然后清空原数据表,最后将临时表数据存回原数据表
--STEP01
SELECT DISTINCT Emp_id, Emp_Name
,Dept_id, Mobile, Ext
,Salary, Email, Date_Update
INTO #Emp_Bak
FROM Emp_Bak
--STEP02
TRUNCATE TABLE Emp_Bak
--STEP03
INSERT INTO Emp_Bak
SELECT Emp_id, Emp_Name
,Dept_id ,Mobile, Ext
,Salary, Email
, GETDATE() Date_Create --
FROM #Emp_Bak
------------------------------4.5------------------------------------
--105, ORACLE, 删除大量数据时 可能导致数据库事务日志文件急剧扩展,甚至无法继续进行事务处理等问题,可用分批删除数据方法
DECLARE
n NUMBER;
BEGIN
LOOP
EXIT WHEN n=0;
DELETE EMP_BAK
WHERE ROWNUM<=5
AND Dept_Id Like 'I%';
n :=SQL%ROWCOUNT;
DBMS_OUTPUT.PUT_LINE( n );
COMMIT;
END LOOP;
END;
--105, SQL SERVER,
WHILE (@@ROWCOUNT>0)
BEGIN
DELETE TOP(5)
FROM Emp_Bak
WHERE Dept_Id Like 'I%'
END
--------------------------------
------------------------------5.1------------------------------------
--117
SELECT
TO_CHAR(DATE'-4712-01-01', 'J') "JDay(Base)"
, TO_CHAR(DATE'2010-01-01', 'J') "JDay"
, (TO_CHAR(DATE'2010-01-01', 'J')
-TO_CHAR(DATE'-4712-01-01', 'J'))/365.25 Diff
FROM DUAL
--117
SELECT DATE'2010-03-17' Today
, TIMESTAMP '2010-3-17 8:28:40' Now
FROM DUAL
------------------------------5.4------------------------------------
--124, ORACLE
SELECT
NVL(A, '新值') "test1.A"
, COALESCE(A, '新值')"test1.B"
, COALESCE(A, B, C) "test2.A"
, NVL(A, NVL(B, C)) "test2.B"
FROM
(
SELECT NULL A
, NULL B
, '非NULL' C
FROM DUAL
)
--124, SQL SERVER
SELECT
ISNULL(A, '新值') "test1.A"
, COALESCE(A, '新值') "test1.B"
, COALESCE(A, B, C) "test2.A"
, ISNULL(A, ISNULL(B, C)) "test2.B"
FROM
(
SELECT NULL A
, NULL B
, '非NULL' C
) A
--125
--SQL SERVER
SELECT VAL
, NULLIF(VAL, 0) "NULLIF(VAL, 0)"
FROM
(
SELECT 0 VAL
--FROM DUAL
UNION ALL
SELECT 20 VAL
--FROM DUAL
) A
--ORACLE
SELECT VAL
, NULLIF(VAL, 0) "NULLIF(VAL, 0)"
FROM
(
SELECT 0 VAL
FROM DUAL
UNION ALL
SELECT 20 VAL
FROM DUAL
) A
--mssql去空格
SELECT REPLACE(REPLACE(REPLACE(REPLACE(CAST('abc def ghi cd e' AS VARCHAR),CHAR(13) + CHAR(10),''),CHAR(13),''),CHAR(10),''),' ','')
-- 查询table1中 city相同数目大于1的 且text字段为空
select * from table1 where city in
(select city from table1 group by city having count(*)>1)
and text is null
--SQL:删除重复数据,只保留一条
--在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢
--1、查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断
select * from people
where peopleId in (select peopleId from people group by peopleId having count(peopleId) > 1)
--2、删除表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断,只留有rowid最小的记录
delete from people
where peopleName in (select peopleName from people group by peopleName having count(peopleName) > 1)
and peopleId not in (select min(peopleId) from people group by peopleName having count(peopleName)>1)
--3、查找表中多余的重复记录(多个字段)
select * from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
--4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录
delete from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)
--5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录
select * from vitae a
where (a.peopleId,a.seq) in (select peopleId,seq from vitae group by peopleId,seq having count(*) > 1)
and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)
--6.消除一个字段的左边的第一位:
update tableName set [Title]=Right([Title],(len([Title])-1)) where Title like '村%'
--7.消除一个字段的右边的第一位:
update tableName set [Title]=left([Title],(len([Title])-1)) where Title like '%村'
--8.假删除表中多余的重复记录(多个字段),不包含rowid最小的记录
update vitae set ispass=-1
where peopleId in (select peopleId from vitae group by peopleId,seq having count(*) > 1) and seq in (select seq from vitae group by peopleId,seq having count(*) > 1)
and rowid not in (select min(rowid) from vitae group by peopleId,seq having count(*)>1)