zoukankan
html css js c++ java
一些sql语句
1.删除表中重复记录,以前是加DISTINCT参数,导出到临时表,再导回来,无意中发现一条语句也可以。例如,去掉学号字段重复的记录:
delete
from
[
table
]
where
ID
not
in
(
select
max
(ID)
as
ID
from
[
table
]
group
by
学号)
2.和上边类似,列出学号重复的记录数:
select
max
(学号),
count
(学号)
from
[
table
]
group
by
学号
having
count
(学号)
>
0
3.列出数据表的字段
select
name
from
syscolumns
where
id
=
object_id
(
'
表名
'
)
4.收缩数据库(会大大减小数据库和日志):
dbcc
shrinkdatabase(数据库名)
5.以前匹配如:2006开头的字段,都用left(字段,4)='2006',据说这样效率低,改写成这样:
select
学号
from
[
表名
]
where
入学时间
like
'
2006%
'
6.table1记录学生密码,table2记录学生信息,根据table2的学号,自动添加没有的学号到table1中
INSERT
INTO
[
table1
]
(学号, 姓名, 密码)
SELECT
学号, 姓名,
'
111111
'
AS
密码
FROM
[
table2
]
WHERE
(
NOT
EXISTS
(
SELECT
学号
FROM
[
table1
]
WHERE
[
table2
]
.学号
=
[
table1
]
.学号))
7.接上一个,删除table1中在table2里没有记录的学号
DELETE
FROM
[
table1
]
WHERE
(
NOT
EXISTS
(
SELECT
学号
FROM
[
table2
]
WHERE
[
table1
]
.学号
=
[
table2
]
.学号))
查看全文
相关阅读:
<SpringMvc>入门二 常用注解
<SpringMvc>入门一 HelloWorld
<MyBatis>入门六 动态sql
<MyBatis>入门五 查询的返回值处理
<MyBatis>入门四 传入的参数处理
<MyBatis>入门三 sqlMapper文件
<MyBatis>入门二 全局配置文件
<MyBatis>入门一 HelloWorld
类和类的继承(6)
python 的重载
原文地址:https://www.cnblogs.com/zwei1121/p/715370.html
最新文章
[OJ] Insert Interval
Flatten Binary Tree to Linked List (LeetCode #114 Medium)(LintCode #453 Easy)
CodePen避免自动刷新导致的JS卡死
[LeetCode] 303. Range Sum Query
[LeetCode] 306. Additive Number [Medium]
[LeetCode] 315. Count of Smaller Numbers After Self (Hard)
[LeetCode] Super Ugly Number (Medium)
[LeetCode] Burst Balloons (Medium)
HashSet和ArrayList有什么区别
Set集合
热门文章
有关String的一些方法的使用
this和super的区别
构造方法
<Closing connections idle longer than 60000 MILLISECONDS> <Closing expired connections>
OSS 实例
java后台判断浏览器的版本
WGS84,GCJ02, BD09坐标转换
部署项目到linux中报Spring MVC报异常:org.springframework.web.util.NestedServletException: Request processing failed
<SpringMvc>入门四 响应结果
<SpringMvc>入门三 参数绑定
Copyright © 2011-2022 走看看