您的位置:首页 > 其它

select 语句的用法(转贴)

2008-12-04 17:46 363 查看
今天学习了一些关于数据库查询语句方面的知识,我想对以后的测试工作应该有一定的帮助,它写的很全面,呵呵.作为保留.

一.以列方式过滤输出

(1)select * from manytable

查询manytable表中的所有字段

(2)通过列方式进行查询

select bh,xm,nl from manytable

二.字段转换查询

(1)将列名以中文形式输出.方法一:

select bh as 编号,xm as 姓名,nl as 年龄 from manytable

(2)将列名以中文形式输出.方法二:

select bh 编号,xm 姓名,nl 年龄 from manytable

三.以行过滤输出

(1)select * from manytable where bh>'003' (基于字符型)

查询'bh'大于'003'的表中所有字段记录

(2)select * from manytable where xm='yan' (基于字符型)

查询'xm'等于'yan'的表中所有字段记录

(3)select * from manytable where nl<=30 (基于数字型)

查询'nl'小于或等于30的表中所有字段记录

(4)select * from manytable where sj>'1999-1-1' (基于时间型)

查询'sj'大于'1999-1-1'的表中所有字段记录

五.模糊查询(相似查询)

*只对字符的数据类型有效

(1)select * from manytable where xm like '%g'

查询'xm'字段中以'g'结尾的所有字段记录

(2)select * from manytable where xm like 'x%'

查询'xm'字段中以'x'开头的所有字段记录

(3)select * from manytable where xm like '%g%'

查询'xm'字段中有'x'的所有字段记录

(4)select * from manytable where xm like '_e%'

查询'xm'字段中第二个字符是'e'的所有字段记录

(5)select * from manytable where xm like '%e_'

查询'xm'字段中倒数第二个字符是'e'的所有字段记录

六.两个条件的查询(字段)

(1)select * from manytable where bh>'002' and bh<'005'

查询'bh'大于'002'并且'bh'小于'005'的所有字段记录

(2)select * from manytable where nl>50 or nl<30

查询'nl'大于50或者'nl'小于30的所有字段记录

(3)select * from manytable where not nl>50

查询'nl'不大于50的所有字段记录

七.多条件查询

(1)select * from manytable where (nl<50 ond nl<30) or xm like '%x%'

查询('nl'小于50和'nl'大于30)或者是'xm'中有'x'字段的所有字段记录

(2)select * from manytable where nl between 30 and 40

查询'nl'在304和40之间值的所有字段记录

(3)select * from manytable where sj between '2000-1-1' and '2003-1-1'

查询'sj'在'2000-1-1'和'2003-1-1'之间的所有字段记录

(4)select * from manytable where nl>30 and bm='b'

查询'nl'大于30并且'bm'等于'b'的所有字段记录

八.对列和行进行同时查询

(1)select bh,xm,nl from manytable where nl >40

查询'nl'大于40的manytable表中bh,xm,nl字段的值

九.统计函数

(1)select sum(xs) from manytable

统计表中'xs'字段的和值输出

(2)select sum(xs) 总计 from manytable

统计表中'xs'字段的和值并以列名以'总计'的显示输出

(3)select avg(xs) from manytable

统计表中'xs'字段的平均值输出

(4)select max(xs) from manytable

统计表中'xs'字段最大值输出

(5)select min(xs) from manytable

统计表中'xs'字段最小值输出

(6)select count(xs) from manytable

统计表中'xs'字段的个数输出

十.根据条件求函数

(1)select sum(xs) from manytable where nl>40

统计表中'nl'大于40的对应的'xs'值的和值输出

(2)select avg(xs) from manytable where nl>40

统计表中'nl'大于40的对应的'xs'值的平均值输出

(3)select max(xs) from manytable where nl>40

统计表中'nl'大于40的对应的'xs'值的最大值输出

(4)select min(xs) from manytable where nl>40

统计表中'nl'大于40的对应的'xs'值的最小值输出

转自:http://bbs.54master.com/viewthread.php?tid=143730
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: