1. 基本语法:

SELECT
{* | <字段列名>}
[
FROM <表 1>, <表 2>…
[WHERE <表达式>
[GROUP BY <group by definition>
[HAVING <expression> [{<operator> <expression>}…]]
[ORDER BY <order by definition>]
[LIMIT[<offset>,] <row count>]
]

 1.1 全部查询

SELECT * FROM < 表名 >;

1.2 选择指定字段

SELECT <字段名1>,<字段名2>,…,<字段名n> FROM <表名>;

1.3 过滤字段中的重复值: DISTINCT

SELECT DISTINCT <字段名> FROM <表名>;

1.4 别名 :AS

给很长的表或者字段可以起个别名,用别名进行查询

mysql> select a.id,a.name from user as a;
+----+------+
| id | name |
+----+------+
| 1 | Wang |
+----+------+

mysql> select id as userID from user;
+--------+
| userID |
+--------+
| 1 |
+--------+

 1.5 限制查询结果的记录数量:LIMIT

<LIMIT> [<位置偏移量>,] <行数>

“位置偏移量”指从哪一行开始显示,是一个可选参数,如果不指定“位置偏移量”,将会从表中的第一条记录开始(第一条记录的位置偏移量是 0,第二条记录的位置偏移量是 1,以此类推);

第二个参数“行数”指示返回的记录条数。

# 查询从第3+1条记录开始的4条数据。
mysql> SELECT * FROM <表名> LIMIT 3,4;

 1.6 排序查询:ORDER BY

ORDER BY {<列名> | <表达式> | <位置>} [ASC|DESC]

ASC    升序
DESC 降序

e.g.

从user中查询所有字段,先按照 balance 字段进行降序排列,再按照 debt 字段升序排序

mysql> select * from user order by balance desc,debt asc;
+----+------+----------+---------+------+
| id | name | password | balance | debt |
+----+------+----------+---------+------+
| 1 | Wang | 1234 | 15000 | 0 |
| 2 | song | 1234 | 3000 | 100 |
| 3 | li | 1234 | 2000 | 0 |
| 4 | zhao | NULL | NULL | NULL |
| 5 | qian | NULL | NULL | NULL |
| 6 | sun | NULL | NULL | NULL |
| 9 | NULL | NULL | NULL | 70 |
| 8 | NULL | NULL | NULL | 80 |
| 7 | Zhou | NULL | NULL | 90 |
+----+------+----------+---------+------+

 1.7 条件查询: WHERE

WHERE <查询条件> {<判定运算1>,<判定运算2>,…}

判断条件:
1. >,<,<=,>=,=,<>,!=,<=> # 注:<=> 判断两个数据是否相等,只是可以和 NULL 进行比较,其他方面和 = 没啥差别。
2. [NOT]LIKE
3. IS [NOT] NULL
4. [NOT] BETWEEN ... AND ...
5. [NOT][REGEXP|RLIKE]

单一查询:

mysql> select balance from user where name = 'Wang';
+---------+
| balance |
+---------+
| 15000 |
+---------+

多条件查询:

mysql> select name,balance from user where balance>=3000 and balance<16000;
+------+---------+
| name | balance |
+------+---------+
| Wang | 15000 |
| song | 3000 |
+------+---------+

1.8 LIKE 模糊查询

<表达式1> [NOT] LIKE <表达式2>

通配符:

1. 百分号:%  : 可以匹配 0 个或多个字符,不匹配 空值。

2. 下划线:_   : 只匹配一个字符。

mysql> select * from user;
+----+------+----------+---------+------+
| id | name | password | balance | debt |
+----+------+----------+---------+------+
| 1 | Wang | 1234 | 15000 | 0 |
| 2 | song | 1234 | 3000 | 100 |
| 3 | li | 1234 | 2000 | 0 |
| 4 | zhao | NULL | NULL | NULL |
| 5 | qian | NULL | NULL | NULL |
| 6 | sun | NULL | NULL | NULL |
| 7 | Zhou | NULL | NULL | 90 |
| 8 | NULL | NULL | NULL | 80 |
| 9 | NULL | NULL | NULL | 70 |
+----+------+----------+---------+------+ mysql> select name,debt from user where name like '%ng';
+------+------+
| name | debt |
+------+------+
| Wang | 0 |
| song | 100 |
+------+------+

1.9 查询日期

select * from <table> where date<'2019-01-20';

select * from <table> where date between '2019-01-20' and '2019-10-01';

2.0 内连接查询

mysql> select * from grade;
+------+--------+
| gid | record |
+------+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 400 |
+------+--------+ mysql> select * from user;
+----+------+----------+---------+------+
| id | name | password | balance | debt |
+----+------+----------+---------+------+
| 1 | Wang | 1234 | 15000 | 0 |
| 2 | song | 1234 | 3000 | 100 |
| 3 | li | 1234 | 2000 | 0 |
| 4 | zhao | NULL | NULL | NULL |
| 5 | qian | NULL | NULL | NULL |
| 6 | sun | NULL | NULL | NULL |
| 7 | Zhou | NULL | NULL | 90 |
| 8 | NULL | NULL | NULL | 80 |
| 9 | NULL | NULL | NULL | 70 |
+----+------+----------+---------+------+ # 内连接查询
mysql> select * from user inner join grade where user.id=grade.gid;
+----+------+----------+---------+------+------+--------+
| id | name | password | balance | debt | gid | record |
+----+------+----------+---------+------+------+--------+
| 1 | Wang | 1234 | 15000 | 0 | 1 | 100 |
| 2 | song | 1234 | 3000 | 100 | 2 | 200 |
| 3 | li | 1234 | 2000 | 0 | 3 | 400 |
+----+------+----------+---------+------+------+--------+ # 不写inner join,默认也是内连接
mysql> select * from user, grade where user.id=grade.gid;
+----+------+----------+---------+------+------+--------+
| id | name | password | balance | debt | gid | record |
+----+------+----------+---------+------+------+--------+
| 1 | Wang | 1234 | 15000 | 0 | 1 | 100 |
| 2 | song | 1234 | 3000 | 100 | 2 | 200 |
| 3 | li | 1234 | 2000 | 0 | 3 | 400 |
+----+------+----------+---------+------+------+--------+ # 指定查询哪些字段
mysql> select user.id,user.name,user.balance,grade.gid from user, grade where user.id=grade.gid;
+----+------+---------+------+
| id | name | balance | gid |
+----+------+---------+------+
| 1 | Wang | 15000 | 1 |
| 2 | song | 3000 | 2 |
| 3 | li | 2000 | 3 |
+----+------+---------+------+

2.1 外连接

左外连接:

基于左边的表,将右边表中的字段合并过来

mysql> select id,name,record from user left join grade on user.id = grade.gid;
+----+------+--------+
| id | name | record |
+----+------+--------+
| 1 | Wang | 100 |
| 2 | song | 200 |
| 3 | li | 400 |
| 4 | zhao | NULL |
| 5 | qian | NULL |
| 6 | sun | NULL |
| 7 | Zhou | NULL |
| 8 | NULL | NULL |
| 9 | NULL | NULL |
+----+------+--------+

右外连接:

mysql> select id,name,record from user right join grade on user.id = grade.gid;
+------+------+--------+
| id | name | record |
+------+------+--------+
| 1 | Wang | 100 |
| 2 | song | 200 |
| 3 | li | 400 |
+------+------+--------+

2.2 子查询

1. IN

<表达式> [NOT] IN <子查询>

此处的子查询只能返回一行数据

2. 比较运算符

<表达式> {= | < | > | >= | <= | <=> | < > | != }
{ ALL | SOME | ANY} <子查询>

3. EXISTS

  判断子查询是否存在,如果存在,True,否则False;

IN:

mysql> select * from grade;
+------+--------+
| gid | record |
+------+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 400 |
+------+--------+ mysql> select * from user;
+----+------+----------+---------+------+
| id | name | password | balance | debt |
+----+------+----------+---------+------+
| 1 | Wang | 1234 | 15000 | 0 |
| 2 | song | 1234 | 3000 | 100 |
| 3 | li | 1234 | 2000 | 0 |
| 4 | zhao | NULL | NULL | NULL |
| 5 | qian | NULL | NULL | NULL |
| 6 | sun | NULL | NULL | NULL |
| 7 | Zhou | NULL | NULL | 90 |
| 8 | NULL | NULL | NULL | 80 |
| 9 | NULL | NULL | NULL | 70 |
+----+------+----------+---------+------+ mysql> select * from user where id in (select gid from grade where gid=2);
+----+------+----------+---------+------+
| id | name | password | balance | debt |
+----+------+----------+---------+------+
| 2 | song | 1234 | 3000 | 100 |
+----+------+----------+---------+------+

EXISTS:

mysql> select * from user where exists (select * from grade where gid=1000);
Empty set (0.00 sec) mysql> select * from user where exists (select * from grade where gid=1);
+----+------+----------+---------+------+
| id | name | password | balance | debt |
+----+------+----------+---------+------+
| 1 | Wang | 1234 | 15000 | 0 |
| 2 | song | 1234 | 3000 | 100 |
| 3 | li | 1234 | 2000 | 0 |
| 4 | zhao | NULL | NULL | NULL |
| 5 | qian | NULL | NULL | NULL |
| 6 | sun | NULL | NULL | NULL |
| 7 | Zhou | NULL | NULL | 90 |
| 8 | NULL | NULL | NULL | 80 |
| 9 | NULL | NULL | NULL | 70 |
+----+------+----------+---------+------+

2.3 分组查询

分组查询查到是每个,相同的数据会合并到一个组里,是看不到的。可以使用聚合函数对每个组的数据进行求和、统计数量等。

mysql> select * from user;
+----+------+----------+---------+------+
| id | name | password | balance | debt |
+----+------+----------+---------+------+
| 1 | Wang | 1234 | 15000 | 0 |
| 2 | song | 1234 | 3000 | 100 |
| 3 | li | 1234 | 2000 | 0 |
| 4 | zhao | NULL | NULL | NULL |
| 5 | qian | NULL | NULL | NULL |
| 6 | sun | NULL | NULL | NULL |
| 7 | Zhou | NULL | NULL | 90 |
| 10 | Wang | NULL | 15000 | NULL |
+----+------+----------+---------+------+ # 按照名字分组,查询每个分组的id,name,balance的总和。sum,聚合函数之一。
mysql> select id,name,sum(balance) from user group by name;
+----+------+--------------+
| id | name | sum(balance) |
+----+------+--------------+
| 1 | Wang | 30000 |
| 2 | song | 3000 |
| 3 | li | 2000 |
| 4 | zhao | NULL |
| 5 | qian | NULL |
| 6 | sun | NULL |
| 7 | Zhou | NULL |
+----+------+--------------+

HAVING

having,给分组添加过滤,筛选符合条件的分组

mysql> select count(name) ,name from user group by name having count(name)>1;
+-------------+------+
| count(name) | name |
+-------------+------+
| 2 | Wang |
+-------------+------+

2.4 正则表达式查询:REGEXP

匹配模式

^    匹配开始字符        '^b'匹配以b开头的字符串

$    匹配结束字符        'b$'匹配以b结束的字符串

.     匹配任意单个字符      

*    匹配前面的字符0次或多次     a*b 匹配 b、ab、aaaaaaaab

+    匹配前面的字符1次或多次    a+b 匹配 ab、aaaaaab

<字符>  匹配包含的字符        <fa> 匹配 afa,fa,bbbfaccc

[]    匹配内部的任意一个字符   [xyz] 匹配 x 或 y 或 z

[^]     匹配不在括号内的字符    [^1-9] 不匹配 1-9

{n,}   匹配前一个字符最少n次

{n,m}  匹配前一个字符最少n次,最多m次。

e.g.

mysql> select * from user where name regexp '^W';
+----+------+----------+---------+------+
| id | name | password | balance | debt |
+----+------+----------+---------+------+
| 1 | Wang | 1234 | 15000 | 0 |
| 10 | Wang | NULL | 15000 | NULL |
+----+------+----------+---------+------+
2 rows in set (0.02 sec)

MySQL Select查询的更多相关文章

  1. MYSQL select查询练习题

    10. 查询Score表中的最高分的学生学号和课程号.(子查询或者排序) select sno,cno from score where degree=(select max(degree) from ...

  2. day41:MYSQL:select查询练习题

    目录 1.表结构 2.创建表和插入数据 3.习题 1.表结构 2.建表和插入数据 # 创建班级表 create table class( cid int primary key auto_increm ...

  3. MySQL select 查询之分组和过滤

    SELECT 语法 SELECT [ALL | DISTINCT] {* | table.* | [table.field1[as alias1][,table.field2[as alias2]][ ...

  4. MySQL select 查询的分页和排序

    SELECT 语法 SELECT [ALL | DISTINCT] {* | table.* | [table.field1[as alias1][,table.field2[as alias2]][ ...

  5. mysql select语句查询流程是怎么样的

    select查询流程是怎么样的 mysql select查询的数据是查询内存里面,如果没有查询的数据没有在内存,就需要mysql的innodb引擎读取磁盘,将数据加载的内存后在读取.这就体现了,mys ...

  6. MYSQL—— 基础入门,select 查询涉及到的关键字组合详解(进阶篇)

    SELECT查询组合使用的关键字很多,首先将最简单常用的关键字进行区分及使用,后续再继续补充............ 以下所有的关键字组合使用,主要以两个表students与students_scor ...

  7. MySQL使用select查询时,在查询结果中增加一个字段并指定固定值

    假设需求是这样的: mysql> desc user; +-------+----------+------+-----+---------+----------------+ | Field ...

  8. mysql的select查询语句

    1.简单查询 mysql> select * from students; +------------+----------+------+------+ | id | sname | sex ...

  9. 转载《mysql 一》:mysql的select查询语句内在逻辑执行顺序

    原文:http://www.jellythink.com/archives/924 我的抱怨 我一个搞应用开发的,非要会数据库,这不是专门的数据库开发人员干的事么?话说,小公司也没有数 据库开发人员这 ...

随机推荐

  1. python 提示 :OverflowError: Python int too large to convert to C long

    一次在使用orm进行联表查询的时候,出现   Python int too large to convert to C long 的问题: 在分析错误之后,在错误最后面提示中有: File " ...

  2. ControlTemplate in WPF —— Menu

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x ...

  3. jqGrid整理笔记

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  4. nginx子配置文件实例

    [root@bogon conf.d]# cat /etc/nginx/conf.d/test6.conf server { listen 8085; server_name 192.168.0.20 ...

  5. Python unittest 之 BeautifulReport可视化报告

    众所周知的报告是HTMLTestRunner,虽然经过众多的大神修改后,功能挺强大的,但这颜值,我就不多说了,大家自己感受下吧 HTMLTestRunner就不多说了,近来发现了一款款式新颖,还不漏油 ...

  6. Linux vim文件编辑器使用

    学习目标: 通过本实验熟练vim的使用. 步骤: 1.将用户家目录的ls结果重定向到vimfile.txt 2.查看rh124第403页实验要求,并完成 参考命令: 复制文件前,需要先建立文件,教材上 ...

  7. oracle 11g sqlplus和plsql developer 乱码解决方案

    ----------------------------------华丽的分隔符---------------------------------- 在cmd终端下运行            中文的话 ...

  8. djangourl进阶

  9. C语言博客作业05

    这个作业属于哪个课程 C语言程序设计II 这个作业要求在那里 https://edu.cnblogs.com/campus/zswxy/CST2019-3/homework/9827 我在这个课程的目 ...

  10. [Python3] 030 常用模块 os

    目录 os 1. os.getcwd() 2. os.chdir() 3. os.listdir() 4. os.makedir() 5. os.system() 6. os.getenv() 7. ...