• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

MySQL数据库查询基础,简单查询,条件查询,对查询结果排序

mysql 搞代码 4年前 (2022-01-09) 30次浏览 已收录 0个评论

一.SELECT语句 SELECT COL1,COL2,….COLn FROM TABLE1,TABLE2,….TABLEn

[WHERE CONDITIONS] — 查询条件

[GROUP BY GROUP_BY_LIST] — 查询结果分组

[HAVING CONDITIONS] — 查询条件-统计结果作为条件

[ORDER BY ORDER_LIST[ASC|DESC] — 查询结果排序

二.简单查询

1.查询表的全部行和列

eg:查询玩家表中全部的行和列

select user_qq,user_name,user_sex,user_birthday,user_mobile from users;

select * from users;

2.查询表的部分列

eg:从玩家表中查询玩家QQ和昵称

select user_qq,user_name from users;

3.别名的使用

eg:从玩家表中查询玩家QQ和昵称,并显示为‘玩家QQ' 和 '玩家昵称'

select user_qq as '玩家QQ',user_name as '玩家昵称' from users;

select user_qq '玩家QQ',user_name '玩家昵称' from users;

4.DISTINCT关键字 -消除结果集中的重复行

eg:显示参与了游戏的玩家QQ,要求参与了多个游戏的玩家不重复显示QQ

select distinct user_qq from scores;

5.LIMIT关键字 -指定结果集中数据的显示范围

eg:显示玩家表中第3至第5条数据

select * from users limit 2,3;

select*from users limit 3 —只显示前三条数据

三.条件查询

1.普通条件查询

语法:SELECT COL_LIST FROM TABLE_NAME [WHERE CONDITION_EXPRESSION]

eg1:查询QQ号为12301

本文来源gaodai.ma#com搞#代!码网_

的玩家信息

select * from users where user_qq =12301;

eg2:查询分数大于2500分的数据

select *from scores where score>2500;

<> —–不等于 >= —–大于等于 <= —–小于等于

eg3:查询游戏编号为1且分数大于4000分的分数信息

select * from scores where gno=1 and score>4000;

逻辑运算符:并且 — and

或者 — or

非 — not

eg4: 查询游戏编号为1和2的分数信息

select * from scores where gno=1 or gno=2;

2.模糊查询

eg1:查询分数在2500(含)到3000(含)的分数

select *from scores where score>=2500 and score<=3000;

select * from scores where score between 2500 and 3000;

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

select * from scores where score not between 2500 and 3000;

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

select * from users where user_birthday between '1987-01-01' and '1992-0731';

通配符: '_' 一个字符 Branch like 'L_'

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

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

[^] 不在括号中 Airbusno Like 'AB0[^]'

eg4:查询所有姓孙的玩家信息

select * from users where user_name like '孙%';

eg5:查询所有非姓孙的玩家信息

select * from users where user_name not like '孙%';

3.查询空值得运算符

eg:查询生日为空的null的玩家信息

select * from users where use_birthday is null;

eg:查询生日不为NULL的玩家信息

select * from users where user_birthday is not null;

四 对查询结果排序

1. 对指定列进行排序(排序依据,排序方式)

语法:SELECT CLO_LIST FROM TABLE_NAME ORDER BY ORDER_BY_LIST [ASC/DESC]

例:查询分数表中编号为1的所有分数信息,并按照分数升序排序

select *from scores where gno=1 order by score asc.

例:查询分数表中编号为1的所有分数信息,并按照分数降序排序

select * from score where gno=1 order by score desc.

2. 对多列进行排序(排序依据,排序方式,优先级)

例:查询分数表中的所有信息,并按照游戏编号的升序和分数的降序进行排序

select * from scores order by gno asc, score desc

以上就是MySQL数据库查询基础,简单查询,条件查询,对查询结果排序的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:MySQL数据库查询基础,简单查询,条件查询,对查询结果排序

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址