600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > 数据库模糊查询和范围查询

数据库模糊查询和范围查询

时间:2023-05-06 08:40:22

相关推荐

数据库模糊查询和范围查询

--查询一张表

select * from 表名--*代表所有

select 列1,列2,列3 from 表名 where 条件

select 列名 as 别名,列名 别名,别名=列名 from 表名--显示中文名称的三种形式

--排序

select 列1,列2,列3 from 表名 order by 列2,列3desc--desc为降序\asc为升序

--模糊查询

(1)%

select * from 表名 where 列名 like '%nm%'

select * from 表名 where 列名 like '%n'--匹配以n结尾

select * from 表名 where 列名 like 'n%'--匹配以n开头

(2)_ 匹配单个字符 限制表达式的字符长度

select * from 表名 where 列名 like '_ad_'--可以匹配出形似sadf的多有数据

select * from 表名 where 列名 like '____'--这里有四个_,可以匹配出所有四个字符的数据

(3)[] 范围匹配 括号中拥有的字符中的一个

select * from 表名 where 列名 like 'a[n|m]d'--可以匹配出and和amd

select * from 表名 where 列名 like 'a[nm]d'--可以省略|

select * from 表名 where 列名 like 'a[n-p]d'--也可以是一个区间

(4)[^] 括号中的字符会被忽略

select * from 表名 where 列名 like ’a[^nm]d‘

--范围查询 select from where 条件

(1)比较运算符 >,<,>=,<=

where No<10 and Id>1--and且 or或

(2)in(,,) not in(,,)

where No in (2,3,4)

select * from Table1 where No in (select No from Table2 where No>1)

(3)between and<-(推荐) 等价于 <=,>=

where No between 1 and 10--No>=1 and No<=10

--百分比查询

select top 100 * from 表名--前100条

select top 50 percent * from 表名--前百分之五十

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。