您的位置:首页 > 其它

查询数据,模糊查询,通配符,比较运算符,distinct,limit关键字

2018-01-12 11:18 399 查看
select语句

select col1,col2....coln

form table1,table2....tablen

[where conditions]

[group by Group_BY_LIST]

[having conditions]

[order by ORDER_LIST[ASC|DESC]]

 

select
列名

这些列来源于哪些表

where 查询条件

用来把查询的结果分组统计(分组之后可以有一个having子句,是统计的结果作为统计)

对查询的结果进行排序

 

查询表的全部行和列(select * form USERS)

select user_qq,user_name,user_sex,user_birthday,user_modify

form USERS

 

查询表的部分列

select user_qq,user_name form USERS

 

别名的使用(但数据库中没有变,其实as可以省略)

select user_qq as '玩家QQ',user_name as '玩家昵称'
form USERS

select user_qq '玩家QQ',user_name '玩家昵称'
form USERS

 

消除结果集中的重复行(distinct关键字),可能有些玩家完了多个游戏,现在我们要查询的只是有分数的玩家,不用重复

select distinct user_qq form scores

 

limit关键字

指定结果集中数据的显示范围

例如:显示玩家表中的第3至5条数据

select * form users limit 2,3

2表示第三条数据,一共显示3条数据

 

普通条件查询

select col_list form table_name

[where condition_expression]

例如:查询qq号为12301的玩家信息

select * form users where user_qq='12301'

例如:查下分数大于2500分的数据

select * form scores where score>2500

比较运算符

等于 =

不等于 <>

大于 >

大于等于 >=

小于 <

小于等于 <=

例如:查下游戏编号为1且分数大于4000分的分数信息(gno游戏编号)

select * form scores where gno=1 AND score>4000

逻辑运算符

并且 AND

或者 OR

非 NOT

例如:查询游戏编号为1和2的分数信息(既要显示1号游戏的信息也要显示2号游戏的信息)

select * form scores where gno=1 OR gno=2

 

模糊查询

例如:查询分数在2500(含)到3000(含)的分数信息

传统写法:select * form scores where score>=2500 and score<=3000

模糊查询:select * form scores where score
between 2500
and 3000

例如:查询分数不在2500(含)到3000(含)的分数信息

select * form scores where score not
between 2500
and 3000

例如:查询1987年1月1日到1992年7月31日出生的玩家

select * form users where user_birthday between '1987-1-1' and '1992-07-31'

通配符

'_'  一个字符     Branch like 'L_'

%    任意长度   Route_Code Like 'AMS-%'

[]   指定范围   Airbusno Like 'ABO[1-5]'

[^]  不再括号中   Airbusno Like 'ABO[^1-5]'

例如:查询所有姓孙的玩家信息(%必须放在like关键字后面才起到任意字符的作用)

select * form USERS where user_name like '孙%'

例如:查询所有不姓孙的玩家信息

select * form USERS where user_name not like '孙%'

 

查询空值的运算符

例如:查询生日为null的玩家信息(不能是=null,也不能是like)

select * form USERS where user_birthday is null

例如:查询生日不为null的玩家信息(not放在is
null中间)

select * form USERS where user_birthday is
not null

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息