您的位置:首页 > 数据库

sql server 2005 学习心得(select查询语句用法)

2014-11-17 15:51 337 查看



select * from userInfo where age like '2[25]'
功能:查询userInfo表中age字段,所有以2开头,且第二位是2或5的记录。
 
select * from userInfo where name like '_娜_'
功能:查询userInfo表中name(char(6))字段所有中间一个字是“娜”的记录。下划线“_”代表的是两个任意字符,如上:名为“谢娜娜”或“谢娜”等的记录将会被查询出来
 
select * from userInfo where address like '%川%'
功能:查询userInfo表中address字段所有包含“川”字的记录
 
select * from userInfo where name like '[m-n]%'
功能:查询所有userInfo表中所有name字段是以m或n开头的记录
 
select * from userInfo where name like 'm[^i]%'
功能:查询userInfo表中name字段所有以m开头、第2个字母不是字母c的名称
 
select * from userInfo where ID in(1,2)
功能:查询userInfo表中所有ID等于1或者2的记录
等同于
select * from userInfo where ID='1' or ID='2'
 
select * from userInfo where memo like '20/%' escape '/'
功能:查询memo字段值为20%的所有记录,但是在sql Server里面%为通配符如果不加就会有其他意思
例如:select * from userInfo where memo like '2%'   就跟上面查到一样的记录,这一句的功能是查到memo字段的值以2开头的记录。20%也是以2开头的。这样就会出现问题。所以用转义字符还是很必要的。
 
select count(distinct ProdID) '产品种类' from userInfo
功能:查询出来有多少种产品,distinct就是查询不重复的记录,加上count()就是不重复的记录的总数了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: