select查询语句

语法

select [选项] 列名 [from 表名] [where 条件] [group by 分组] [order by 排序][having 条件] [limit 限制]

select字段表达式

可以直接输出内容

MariaDB [sel]> select 'Sunny is a boy!';

+-----------------+

| Sunny is a boy! |

+-----------------+

| Sunny is a boy! |

+-----------------+

# `1 row in set (0.000 sec)`

输出表达式

MariaDB [sel]> select 10*27;

+-------+

| 10*27 |

+-------+

| 270 |

+-------+

# `1 row in set (0.000 sec)`

MariaDB [sel]> select name,chinese,math,chinese+math from grades;

+-------+---------+------+--------------+

| name | chinese | math | chinese+math |

+-------+---------+------+--------------+

| Sunny | 93 | 96 | 189 |

| Jerry | 97 | 91 | 188 |

| Marry | 95 | 94 | 189 |

| Tommy | 98 | 94 | 192 |

+-------+---------+------+--------------+

# `4 rows in set (0.000 sec)`

输出函数表达式

MariaDB [sel]> select rand();

+-------------------+

| rand() |

+-------------------+

| 0.294372641755615 |

+-------------------+

# `1 row in set (0.007 sec)`

通过as给字段取别名

as可以省略

MariaDB [sel]> select 'Sunny' as 'name';

+-------+

| name |

+-------+

| Sunny |

+-------+

# `1 row in set (0.000 sec)`

MariaDB [sel]> select name,chinese,math,chinese+math as '总分' from grades;

+-------+---------+------+------+

| name | chinese | math | 总分 |

+-------+---------+------+------+

| Sunny | 93 | 96 | 189 |

| Jerry | 97 | 91 | 188 |

| Marry | 95 | 94 | 189 |

| Tommy | 98 | 94 | 192 |

+-------+---------+------+------+

# `4 rows in set (0.007 sec)`

MariaDB [sel]> select name,chinese,math,chinese+math '总分' from grades;

+-------+---------+------+------+

| name | chinese | math | 总分 |

+-------+---------+------+------+

| Sunny | 93 | 96 | 189 |

| Jerry | 97 | 91 | 188 |

| Marry | 95 | 94 | 189 |

| Tommy | 98 | 94 | 192 |

+-------+---------+------+------+

# `4 rows in set (0.001 sec)`

from子句

from [表名]

from后面跟的是数据源

数据源可以有多个,返回笛卡尔积

-- 创建数据表

MariaDB [sel]> create table stu1(

-> name varchar(20)

-> )charset=gbk;

# `Query OK, 0 rows affected (0.026 sec)`

MariaDB [sel]> insert into stu1 values ('Sunny'),('Jerry');

# `Query OK, 2 rows affected (0.012 sec)`

# `Records: 2 Duplicates: 0 Warnings: 0`

MariaDB [sel]> create table stu2(

-> age int

-> )charset=gbk;

# `Query OK, 0 rows affected (0.023 sec)`

MariaDB [sel]> insert into stu2 values (20),(24);

# `Query OK, 2 rows affected (0.012 sec)`

# `Records: 2 Duplicates: 0 Warnings: 0`

-- from子句查询

MariaDB [sel]> select * from stu1;

+-------+

| name |

+-------+

| Sunny |

| Jerry |

+-------+

# `2 rows in set (0.000 sec)`

-- from返回笛卡尔积

MariaDB [sel]> select * from stu1,stu2;

+-------+------+

| name | age |

+-------+------+

| Sunny | 20 |

| Jerry | 20 |

| Sunny | 24 |

| Jerry | 24 |

+-------+------+

# `4 rows in set (0.000 sec)`

dual表

概念

dual表是一个伪表

在有些特定情况下,没有具体的表的参与

为了保证select语句的完整又必须要一个表名,这时候就使用伪表

MariaDB [sel]> select 10*27 as '结果' from dual;

+------+

| 结果 |

+------+

| 270 |

+------+

# `1 row in set (0.007 sec)`

where子句

概念

where后面跟的是条件,在数据源中进行筛选

返回条件为真记录

MySQL支持的运算符

比较运算符

> 大于

< 小于

>= 大于等于

<= 小于等于

= 等于

!= 不等于

逻辑运算符

and 与

or 或

not 非

其他

in | not in 字段的值在枚举范围内

between...and | not between...and 字段的值在数字范围内

is null | is not null 字段的值不为空

-- 比较运算判断

MariaDB [sel]> select * from grades where math=94;

+-------+---------+------+

| name | chinese | math |

+-------+---------+------+

| Marry | 95 | 94 |

| Tommy | 98 | 94 |

+-------+---------+------+

# `2 rows in set (0.007 sec)`

-- 输出所有数据

MariaDB [sel]> select * from grades where 1;

+-------+---------+------+

| name | chinese | math |

+-------+---------+------+

| Sunny | 93 | 96 |

| Jerry | 97 | 91 |

| Marry | 95 | 94 |

| Tommy | 98 | 94 |

+-------+---------+------+

# `4 rows in set (0.000 sec)`

-- 不输出数据

MariaDB [sel]> select * from grades where 0;

# `Empty set (0.000 sec)`

-- 逻辑运算判断

MariaDB [sel]> select * from grades where math=96 or math=91;

+-------+---------+------+

| name | chinese | math |

+-------+---------+------+

| Sunny | 93 | 96 |

| Jerry | 97 | 91 |

+-------+---------+------+

# `2 rows in set (0.001 sec)`

MariaDB [sel]> select * from grades where math in (91,96);

+-------+---------+------+

| name | chinese | math |

+-------+---------+------+

| Sunny | 93 | 96 |

| Jerry | 97 | 91 |

+-------+---------+------+

# `2 rows in set (0.000 sec)`

MariaDB [sel]> select * from grades where math not in (91,96);

+-------+---------+------+

| name | chinese | math |

+-------+---------+------+

| Marry | 95 | 94 |

| Tommy | 98 | 94 |

+-------+---------+------+

# `2 rows in set (0.000 sec)`

查找年龄在20~25之间

-- 方法一:

mysql> select * from stu where stuage>=20 and stuage<=25;

-- 方法二:

mysql> select * from stu where not(stuage<20 or stuage>25);

-- 方法三:between...and...

mysql> select * from stu where stuage between 20 and 25;

-- 年龄不在20~25之间

mysql> select * from stu where stuage not between 20 and 25;

查找缺考的学生

mysql> select * from stu where ch is null or math is null;

+--------+----------+--------+--------+---------+------------+------+------+

| stuNo | stuName | stuSex | stuAge | stuSeat | stuAddress | ch | math |

+--------+----------+--------+--------+---------+------------+------+------+

| s25301 | 张秋丽 | 男 | 18 | 1 | 北京 | 80 | NULL |

| s25304 | 欧阳俊雄 | 男 | 28 | 4 | 天津 | NULL | 74 |

+--------+----------+--------+--------+---------+------------+------+------+

查找没有缺考的学生

mysql> select * from stu where ch is not null and math is not null;

+--------+----------+--------+--------+---------+------------+------+------+

| stuNo | stuName | stuSex | stuAge | stuSeat | stuAddress | ch | math |

+--------+----------+--------+--------+---------+------------+------+------+

| s25302 | 李文才 | 男 | 31 | 3 | 上海 | 77 | 76 |

| s25303 | 李斯文 | 女 | 22 | 2 | 北京 | 55 | 82 |

| s25305 | 诸葛丽丽 | 女 | 23 | 7 | 河南 | 72 | 56 |

| s25318 | 争青小子 | 男 | 26 | 6 | 天津 | 86 | 92 |

| s25319 | 梅超风 | 女 | 23 | 5 | 河北 | 74 | 67 |

| s25320 | Tom | 男 | 24 | 8 | 北京 | 65 | 67 |

| s25321 | Tabm | 女 | 23 | 9 | 河北 | 88 | 77 |

+--------+----------+--------+--------+---------+------------+------+------+

# `7 rows in set (0.00 sec)`

查找需要补考的学生

mysql> select * from stu where ch<60 or math<60 or ch is null or math is null;

+--------+----------+--------+--------+---------+------------+------+------+

| stuNo | stuName | stuSex | stuAge | stuSeat | stuAddress | ch | math |

+--------+----------+--------+--------+---------+------------+------+------+

| s25301 | 张秋丽 | 男 | 18 | 1 | 北京 | 80 | NULL |

| s25303 | 李斯文 | 女 | 22 | 2 | 北京 | 55 | 82 |

| s25304 | 欧阳俊雄 | 男 | 28 | 4 | 天津 | NULL | 74 |

| s25305 | 诸葛丽丽 | 女 | 23 | 7 | 河南 | 72 | 56 |

+--------+----------+--------+--------+---------+------------+------+------+

4 rows in set (0.00 sec)

查找学号是s25301,s25302,s25303的学生

mysql> select * from stu where stuno in ('s25301','s25302','s25303');

+--------+---------+--------+--------+---------+------------+------+------+

| stuNo | stuName | stuSex | stuAge | stuSeat | stuAddress | ch | math |

+--------+---------+--------+--------+---------+------------+------+------+

| s25301 | 张秋丽 | 男 | 18 | 1 | 北京 | 80 | NULL |

| s25302 | 李文才 | 男 | 31 | 3 | 上海 | 77 | 76 |

| s25303 | 李斯文 | 女 | 22 | 2 | 北京 | 55 | 82 |

+--------+---------+--------+--------+---------+------------+------+------+

# `3 rows in set (0.00 sec)`

查找年龄是18~20的学生

mysql> select * from stu where stuage between 18 and 20;

+--------+---------+--------+--------+---------+------------+------+------+

| stuNo | stuName | stuSex | stuAge | stuSeat | stuAddress | ch | math |

+--------+---------+--------+--------+---------+------------+------+------+

| s25301 | 张秋丽 | 男 | 18 | 1 | 北京 | 80 | NULL |

+--------+---------+--------+--------+---------+------------+------+------+

1 row in set (0.00 sec)

group by [分组查询]

概念

将查询的结果分组,分组查询目的在于统计数据

如果是分组查询,查询字段是分组字段和聚合函数

查询字段是普通字段,只取第一个值

group_concat()将同一组的数据连接起来

-- 查询男生和女生的各自语文平均分

mysql> select stusex,avg(ch) '平均分' from stu group by stusex;

+--------+---------+

| stusex | 平均分 |

+--------+---------+

| 女 | 72.2500 |

| 男 | 77.0000 |

+--------+---------+

# `2 rows in set (0.00 sec)`

-- 查询男生和女生各自多少人

mysql> select stusex,count(*) 人数 from stu group by stusex;

+--------+------+

| stusex | 人数 |

+--------+------+

| 女 | 4 |

| 男 | 5 |

+--------+------+

# `2 rows in set (0.00 sec)`

-- 查询每个地区多少人

mysql> select stuaddress,count(*) from stu group by stuaddress;

+------------+----------+

| stuaddress | count(*) |

+------------+----------+

| 上海 | 1 |

| 北京 | 3 |

| 天津 | 2 |

| 河北 | 2 |

| 河南 | 1 |

+------------+----------+

# `5 rows in set (0.00 sec)`

-- 每个地区的数学平均分

mysql> select stuaddress,avg(math) from stu group by stuaddress;

+------------+-----------+

| stuaddress | avg(math) |

+------------+-----------+

| 上海 | 76.0000 |

| 北京 | 74.5000 |

| 天津 | 83.0000 |

| 河北 | 72.0000 |

| 河南 | 56.0000 |

+------------+-----------+

# `5 rows in set (0.00 sec)`

group_concat() 函数

将同一组的值连接起来显示

mysql> select group_concat(stuname),stusex,avg(math) from stu group by stusex;

+-------------------------------------+--------+-----------+

| group_concat(stuname) | stusex | avg(math) |

+-------------------------------------+--------+-----------+

| 李斯文,诸葛丽丽,梅超风,Tabm | 女 | 70.5000 |

| 张秋丽,李文才,欧阳俊雄,争青小子,Tom | 男 | 77.2500 |

+-------------------------------------+--------+-----------+

# `2 rows in set (0.00 sec)`

多列分组

mysql> select stuaddress,stusex,avg(math) from stu group by stuaddress,stusex;

+------------+--------+-----------+

| stuaddress | stusex | avg(math) |

+------------+--------+-----------+

| 上海 | 男 | 76.0000 |

| 北京 | 女 | 82.0000 |

| 北京 | 男 | 67.0000 |

| 天津 | 男 | 83.0000 |

| 河北 | 女 | 72.0000 |

| 河南 | 女 | 56.0000 |

+------------+--------+-----------+

# `6 rows in set (0.00 sec)`

order by [排序]

asc 升序 [默认]

desc 降序

MariaDB [sel]> select * from grades order by math desc;

+-------+---------+------+

| name | chinese | math |

+-------+---------+------+

| Sunny | 93 | 96 |

| Marry | 95 | 94 |

| Tommy | 98 | 94 |

| Jerry | 97 | 91 |

+-------+---------+------+

# `4 rows in set (0.001 sec)`

-- 按总分降序排列

MariaDB [sel]> select *,chinese+math from grades order by math+chinese desc;

+-------+---------+------+--------------+

| name | chinese | math | chinese+math |

+-------+---------+------+--------------+

| Tommy | 98 | 94 | 192 |

| Sunny | 93 | 96 | 189 |

| Marry | 95 | 94 | 189 |

| Jerry | 97 | 91 | 188 |

+-------+---------+------+--------------+

# `4 rows in set (0.001 sec)`

MariaDB [sel]> select *,chinese+math '总分' from grades order by math+chinese desc;

+-------+---------+------+------+

| name | chinese | math | 总分 |

+-------+---------+------+------+

| Tommy | 98 | 94 | 192 |

| Sunny | 93 | 96 | 189 |

| Marry | 95 | 94 | 189 |

| Jerry | 97 | 91 | 188 |

+-------+---------+------+------+

# `4 rows in set (0.000 sec)`

多列排序

前列为主,后列为辅

MariaDB [sel]> select * from grades order by math desc,chinese asc;

+-------+---------+------+

| name | chinese | math |

+-------+---------+------+

| Sunny | 93 | 96 |

| Marry | 95 | 94 |

| Tommy | 98 | 94 |

| Jerry | 97 | 91 |

+-------+---------+------+

# `4 rows in set (0.000 sec)`

having条件

概念

是在结果集上进行条件筛选

having和where的区别

where是对原始数据进行筛选

having是对记录集进行筛选

-- having与where相同

MariaDB [sel]> select * from grades where math=94;

+-------+---------+------+

| name | chinese | math |

+-------+---------+------+

| Marry | 95 | 94 |

| Tommy | 98 | 94 |

+-------+---------+------+

# `2 rows in set (0.000 sec)`

MariaDB [sel]> select * from grades having math=94;

+-------+---------+------+

| name | chinese | math |

+-------+---------+------+

| Marry | 95 | 94 |

| Tommy | 98 | 94 |

+-------+---------+------+

# 2 rows in set (0.000 sec)

-- having与where不同

MariaDB [sel]> select name from grades where math=94;

+-------+

| name |

+-------+

| Marry |

| Tommy |

+-------+

# `2 rows in set (0.000 sec)`

MariaDB [sel]> select name from grades having math=94;

# `ERROR 1054 (42S22): Unknown column 'math' in 'having clause'`

limit

语法

limit [起始位置],显示长度

-- 从第0个位置开始取,取3条记录

mysql> select * from stu limit 0,3;

-- 从第2个位置开始取,取3条记录

mysql> select * from stu limit 2,3;

+--------+----------+--------+--------+---------+------------+------+------+

| stuNo | stuName | stuSex | stuAge | stuSeat | stuAddress | ch | math |

+--------+----------+--------+--------+---------+------------+------+------+

| s25303 | 李斯文 | 女 | 22 | 2 | 北京 | 55 | 82 |

| s25304 | 欧阳俊雄 | 男 | 28 | 4 | 天津 | NULL | 74 |

| s25305 | 诸葛丽丽 | 女 | 23 | 7 | 河南 | 72 | 56 |

+--------+----------+--------+--------+---------+------------+------+------+

# `3 rows in set (0.00 sec)`

起始位置可以省略,默认是从0开始

mysql> select * from stu limit 3;

+--------+---------+--------+--------+---------+------------+------+------+

| stuNo | stuName | stuSex | stuAge | stuSeat | stuAddress | ch | math |

+--------+---------+--------+--------+---------+------------+------+------+

| s25301 | 张秋丽 | 男 | 18 | 1 | 北京 | 80 | NULL |

| s25302 | 李文才 | 男 | 31 | 3 | 上海 | 77 | 76 |

| s25303 | 李斯文 | 女 | 22 | 2 | 北京 | 55 | 82 |

+--------+---------+--------+--------+---------+------------+------+------+

# `3 rows in set (0.00 sec)`

找出班级总分前三名

mysql> select *,ch+math total from stu order by (ch+math) desc limit 0,3;

+--------+----------+--------+--------+---------+------------+------+------+-------+

| stuNo | stuName | stuSex | stuAge | stuSeat | stuAddress | ch | math | total |

+--------+----------+--------+--------+---------+------------+------+------+-------+

| s25318 | 争青小子 | 男 | 26 | 6 | 天津 | 86 | 92 | 178 |

| s25321 | Tabm | 女 | 23 | 9 | 河北 | 88 | 77 | 165 |

| s25302 | 李文才 | 男 | 31 | 3 | 上海 | 77 | 76 | 153 |

+--------+----------+--------+--------+---------+------------+------+------+-------+

# `3 rows in set (0.00 sec)`

limit在update和delete语句中也是可以使用的

-- 前3名语文成绩加1分

mysql> update stu set ch=ch+1 order by ch+math desc limit 3;

# `Query OK, 3 rows affected (0.00 sec)`

# `Rows matched: 3 Changed: 3 Warnings: 0`

-- 前3名删除

mysql> delete from stu order by ch+math desc limit 3;

# `Query OK, 3 rows affected (0.00 sec)`

查询语句中的选项distinct

查询语句中的选项有两个

all 显示所有数据 [默认]

distinct 去除结果集中重复的数据

-- 显示列所有数据

MariaDB [sel]> select all math from grades;

+------+

| math |

+------+

| 96 |

| 91 |

| 94 |

| 94 |

+------+

# `4 rows in set (0.001 sec)`

-- 单列去除重复的项

MariaDB [sel]> select distinct math from grades;

+------+

| math |

+------+

| 96 |

| 91 |

| 94 |

+------+

# `3 rows in set (0.001 sec)`

mysql 伪表查询语句_MySQL数据库之select查询语句相关推荐

  1. mysql工具记录用户的查询语句_MySQL数据库的常用命令语句记录——安全用户语句及函数...

    安全用户语句及函数 CREATE USER:创建用户 CREATE USER 'user'[@'host'] [ IDENTIFIED BY [PASSWORD] 'password'][,....] ...

  2. mysql api查询例子_MySQL数据库:常用查询的例子Frommysqlapi

    本文主要向大家介绍了MySQL数据库的常用查询的例子Frommysqlapi,通过具体的实例让大家了解,希望对大家学习MySQL数据库有所帮助. 常用查询的例子 下面是一些学习如何用MySQL解决一些 ...

  3. mysql select查询字段_MySQL基础:SELECT查询语句

    Blog:博客园 个人 概述 SELECT语句用于从表中选取/查询数据,结果被存储在一个结果表中(称为结果集). 语法格式 SELECT [ALL | DISTINCT | DISTINCTROW ] ...

  4. web mysql 界面表命名规范_MySql数据库表字段命名及设计规范

    1.设计原则 1) 标准化和规范化web 数据的标准化有助于消除数据库中的数据冗余.标准化有好几种形式,但 Third Normal Form(3NF)一般被认为在性能.扩展性和数据完整性方面达到了最 ...

  5. mysql 嵌套查询性能_MySQL数据库之嵌套查询与连接查询的性能详解

    本文主要向大家介绍了MySQL数据库之嵌套查询与连接查询的性能详解 ,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助. 嵌套查询与连接查询的性能:连接查询一般较快:子查询很难被优化. ...

  6. mysql数据库替换语句_Mysql数据库字符串替换常用语句

    站编辑工作有时需要对以前更新的某些内容进行修改,如果只是简单的几条数据,我们只需要直接去网站后台或者MYSQL数据表中修改即可,但如果需要批量修改大量同一字符串时,就需要灵活的选用MYSQL语句进行批 ...

  7. mysql查询语句习题._MySql数据库基本select查询语句练习题,初学者易懂。

    在数据库建立四个表:分别为 student(sid,sname,sage,ssex) teacher(tid,tname) course(cid,cname,tid) sc(sid,cid,score ...

  8. mysql数据库高级查询笔记_MySQL数据库基础——高级查询

    MySQL数据库入门--day08 高级查询 一.聚合函数: 在实际开发过程中经常需要对数据进行统计,为此MySQL中提供了一些函数来实现某些功能如下表所示: 聚合函数 1.COUNT()函数: CO ...

  9. mysql 备份表和数据_Mysql数据库备份(一)------数据库备份和表备份

    一.Mysql中的数据备份: Mysql中数据备份使用的命令是:mysqldump命令将数据库中的数据备份成一个文本文件.表的结构和表中的数据将存储在生成的文本文件中.mysqldump命令的 工作原 ...

最新文章

  1. C++ Primer 5th笔记(chap 18 大型程序工具)内联命名空间 (inline namespace)
  2. Educational Codeforces Round 32
  3. UVC (USB Video Class) 使用笔记 (转)
  4. 找出1000以内的完数,所谓完数是指该数的各因子之和等于该数,如:6 = 1+2+3。
  5. ppt转html格式
  6. PASTE Splay
  7. JavaSE之Java基础(1)
  8. python-scrapy爬虫框架
  9. java nio集群_java – Hazelcast:连接到远程集群
  10. ubuntu 上NVIDIA驱动和CUDA9.0 的坑之一二
  11. linux 挂载u盘考试,Linux 挂载U盘,与解挂
  12. Mysql合并两个sql结果
  13. 华为云发布国内首个 AI 模型市场,加速企业 AI 应用落地
  14. c语言如何调用三个子程序,哪位师傅知道51单片机怎样编写子程序?C语言的。在主程序里调...
  15. 新建一个包,并生成可以直接在命令行执行的指令
  16. ConurrentHashMap和Hashtable的区别
  17. HTML+CSS登陆界面实例
  18. 地图,GPS位置地图坐标系:WGS-84(GPS)、GCJ-02(Google地图)、BD-09(百度地图),OpenGIS
  19. GSM和GPRS有什么区别
  20. 基于改进YOLOv7和CRNN的管道裂缝检测系统(源码&教程)

热门文章

  1. 干货!考博英语,这样复习可以事半功倍
  2. Cesium设置模型朝向速度矢量方向
  3. php将手机号转换成QQ号,php生成像qq号码这种的唯一数字id?
  4. C语言关于函数学习的总结
  5. 一个集合了497个AI工具的人工智能网站
  6. 阿里开发人员献礼“Java架构成长笔记”,深入内核,拒绝蒙圈
  7. 系统权限服务创建桌面进程(进程也是系统权限)
  8. 作为程序员,我一直觉得修身养性才是根本,技能牛逼是其次!
  9. php token 失效时间,ThinkPHP5.1表单令牌Token失效问题的解决_php实例 - PHP
  10. 前端小风车 HTML,CSS,Javascript