SQL经典短小代码收集 1
--
SQL Server:
Select
TOP
N
*
from
TABLE
Order
By
NewID
()
--
Access:
Select
TOP
N
*
from
TABLE
Order
By
Rnd(ID)
Rnd(ID) 其中的ID是自动编号字段,可以利用其他任何数值来完成,比如用姓名字段(UserName)
Select
TOP
N
*
from
TABLE
Order
BY
Rnd(
Len
(UserName))
--
MySql:
Select
*
from
TABLE
Order
By
Rand
() Limit
10
--
开头到N条记录
Select
Top
N
*
from
表
--
N到M条记录(要有主索引ID)
Select
Top
M
-
N
*
from
表Where ID
in
(
Select
Top
M ID
from
表)
Order
by
ID
Desc
--
选择10从到15的记录
select
top
5
*
from
(
select
top
15
*
from
table
order
by
id
asc
) table_别名order
by
id
desc
--
N到结尾记录
Select
Top
N
*
from
表Order
by
ID
Desc
--
显示最后5条记录,但是显示的顺序必须为5,6,7,8,9,10,而不是10,9,8,7,6,5 如下解决方法:
select
top
5
from
test
where
id
in
(
select
top
5
from
test
order
by
id
desc
)
order
by
id
asc
--
通过这个问题也能总结出4-10条,5-100条这种限定一定范围内的sql语句的写法:
select
top
<
末端ID
-
顶端ID
+
1
>
*
from
<
表名
>
where
ID
not
in
(
select
top
<
顶端ID
-
1
>
) ID
from
<
表名
>
)
--
例如:4-10条就应该写成
select
top
10
-
4
+
1
*
from
test
where
id
相关文档:
SQL注入就不用介绍了,网上很多。下面介绍一下防止
SQL注入的方法。
使用
quotename 函数和
sp_executesql
参考如
下表结构:这是一个文档表里面有一些简单的字段信息
CREATE
TABLE
[dbo]
.
[DocumentInfo]
(
[ID]
[int]
IDENTITY
(
1,
1) primary key
NOT
......
今天调试服务器,创建了一个新的数据库用户,想给他分配些权限,管理其中的一个数据库。但是等一切都创建好了,却发现他连登陆都没办法登陆。奇怪,dba的角色都给了还是不行。
查看问题,找出解决方案:
企业管理器(2000)或者mangement studio连接你的实例--右键实例--属性--安全性里面看看身份验证模式是否为"sql server ......
使用SQL SERVER2005的时候遇到了中文字符为乱码的情况,经过研究发现,设置SQL的排序规则可以解决这个问题。
1、登录服务器打开Microsoft SQL Server Management Studio。
2、在要修改的数据库上单击鼠标右键,并选择“属性”。
3、在弹出的数据库属性窗口中点击“选择页”中的“选项”。 ......
SELECT id,ip,from_unixtime(last_task_request_time) t1, from_unixtime(last_task_finish_time) t2
from yq_nodemanage
WHERE node_type=1
ORDER BY t1 DESC;
SELECT sum(unix_timestamp(gather_time)-unix_timestamp(publish_time))/(count(*)*60) from yq_bbs_docinfo
WHERE unix_timestamp(publish_time)>un ......
现在遇到了个数据库查找的问题,连接查找,现在有三个表users 表,sex表,languages表,sex表中的lang_id 和motherlang_id是主键外键关系
图片:
联合查找信息时
如果信息完整的话是可以查找出来的,但是信息不完整的话就差找不出来。(如 用户tanaka就无法查出信息)查找语句如下:
select users.id,username,sex_name ......