MySQL札记9_DQL(数据查询语言)

DQL(data query language)数据查询语言,主要的作用是对数据库中的数据进行查询的操作,也是最重要的功能,查询的方法也是多种多样:联合查询、分组查询、内连查询、子查询等,还可以限制查询的条数等,下面一一介绍。

复习创建表

先创建两个表:studentmark表,其中student表中存储的学生姓名、年纪等信息,mark表中存储的是分数和学生学号等信息

  • student
+----+-----------+-------------------+----------+--------------------------------+--------+
| id | user_name | user_email        | user_age | password                       | fee    |
+----+-----------+-------------------+----------+--------------------------------+--------+
|  1 | peter     | 123456a@163.com   |       27 | *C3BC3E91915DCAE22014892F9827D |  28.87 |
|  2 | jim       | 19883728@qq.com   |       22 | *3013993DF5AB62B1310BFB1A4257D |  90.18 |
|  3 | Tom       | 198876728@qq.com  |       32 | *438030B7A94B726F59997202525B5 |  77.18 |
|  4 | Rose      | 7817383728@qq.com |       23 | *3DB09C49A1B49D6AF3B3EF807B91F |  55.45 |
|  5 | zhuliye   | 75683728@qq.com   |       27 | *B688A0F40FDA5181039DACA63C33B |  56.18 |
|  6 | John      | 4532283728@qq.com |       26 | *024D34153E31E0CFBCD619D701232 | 100.18 |
+----+-----------+-------------------+----------+--------------------------------+--------+
  • mark
# 建表
mysql> create table mark(
         > id int unsigned not null auto_increment primary key, 
         > mark int(20) not null comment "score", 
         > stu_id varchar(20) not null comment "student_id" 
         > );

# 查看表结构
mysql> desc mark;
+--------+------------------+------+-----+---------+----------------+
| Field  | Type             | Null | Key | Default | Extra          |
+--------+------------------+------+-----+---------+----------------+
| id     | int(10) unsigned | NO   | PRI | NULL    | auto_increment |
| mark   | int(20)          | NO   |     | NULL    |                |
| stu_id | varchar(20)      | NO   |     | NULL    |                |
+--------+------------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

# 插入数据
mysql> insert into mark values(1, 66, "2");
Query OK, 1 row affected (0.00 sec)

mysql> insert into mark values(2, 75, "3");
Query OK, 1 row affected (0.01 sec)

mysql> insert into mark values(3, 88, "4");
Query OK, 1 row affected (0.01 sec)

mysql> insert into mark values(4, 90, "5");
Query OK, 1 row affected (0.00 sec)

mysql> insert into mark values(5, 82, "6");
Query OK, 1 row affected (0.00 sec)

mysql> select * from mark;
+----+------+--------+
| id | mark | stu_id |
+----+------+--------+
|  1 |   66 | 2      |
|  2 |   75 | 3      |
|  3 |   88 | 4      |
|  4 |   90 | 5      |
|  5 |   82 | 6      |
+----+------+--------+
5 rows in set (0.00 sec)

普通查询

where普通查询

带上where条件进行查询,条件可以是指定id或者其他字段。

mysql> select password, fee from student where id=3;   
+--------------------------------+-------+
| password                       | fee   |
+--------------------------------+-------+
| *438030B7A94B726F59997202525B5 | 77.18 |
+--------------------------------+-------+
1 row in set (0.02 sec)

mysql> select password from student where user_name="Tom";
+--------------------------------+
| password                       |
+--------------------------------+
| *438030B7A94B726F59997202525B5 |
+--------------------------------+
1 row in set (0.00 sec)

过滤查询

.如果某个表中,某些字段的值有重复,可以进行过滤查询。过滤重复字段的关键词是distinctstudent表中的age字段有重复值:

image.png

mysql> select distinct user_age from student;   # 括号有没有均可
mysql> select distinct(user_age) from student;    
image.png

连接查询

连接查询的关键词是concat,有三种不同的使用情况:

  • 直接使用concat
  • 使用concat....as....;显示出来的时候用as后面的别名
  • 带上连接符号的查询concat_ws("==", 列名1,列名2);其中"=="就是指定连接符
image.png
image.png
image.png

模糊查询

模糊查询的关键字是like,中文翻译成

mysql> select user_name from student where user_name like "peter";    # 像peter
mysql> select user_name from student where user_name like "%e";      # %表示任意,表示名字以e结尾
mysql> select user_name from student where user_name like "%e%";    # 表示名字中含有e 
image.png

排列

对表中的记录进行升序asc或者降序desc的排列,默认的是升序asc

mysql> select * from student order by user_age asc;   # 年龄的升序
mysql> select * from student order by user_age desc;  # 年龄的降序
image.png

聚合函数

select count(*) from student;    # 总记录
select sum(列名) from student;  # 总和
select avg(列名) from student;   # 平均值
select max/min(列名) from student;  # 最大/小值

高级查询

分组查询

分组查询中主要使用的是group by,通过by后面的条件来筛选,同时会有一个排序(默认是升序)和去重的功能存在,从而进行查询:

mysql> select * from student group by user_age;
image.png
  • 年纪中有重复的值,对比上图,进行去重显示:


    image.png
  • 根据年龄统计个数,并且显示user_name 和别名totals

    image.png

  • 通过group by ...having...联合进行查询

mysql> select user_name, fee from student group by user_age having count(*)=1;

mysql> select user_name, fee from student group by user_age having count(*)=2;
image.png

内连查询

仔细看看笔者之前创建的两个表,student表中有学生的各种信息,mark表中有个分数和学生编号,请问如何通过这两个表,找到student表中对应mark表里面stu_id的成绩呢?内连查询解决的就是这个问题

image.png

mysql> select user_name, mark from student, mark where mark.stu_id = student.id;      #   通过指定mark.stu_id = student.id 作为限定条件 
+-----------+------+
| user_name | mark |
+-----------+------+
| jim       |   66 |
| Tom       |   75 |
| Rose      |   88 |
| zhuliye   |   90 |
| John      |   82 |
+-----------+------+
5 rows in set (0.00 sec)
  • 使用别名
# 适合两个表中没有想用字段
mysql> select user_name, mark from student as s, mark as m where m.stu_id = s.id;   

# 适合两个表中具有相同字段使用:一定要指明哪个字段属于那个表
mysql> select s.user_name, m.mark from student as s, mark as m where m.stu_id = s.id;
image.png
  • inner join查询
    将上面两个表之间的逗号换成inner join
mysql> select s.user_name, m.mark from student as s inner join mark as m where m.stu_id = s.id;

image.png

where换成on

  • where可以换成on
  • on不能和逗号连用
  • on只能和inner join连用
    image.png

左右连接

  • 左连接
mysql> select s.user_name, m.mark from student as s left join mark as m on m.stu_id = s.id;
  • 右连接
mysql> select s.user_name, m.mark from student as s right join mark as m on m.stu_id = s.id;

联合查询

联合查询的关键字是union all,需要注意的是查询的时候字段需要保持一致。

image.png

  • where条件的拆分
mysql> select * from student where id in (2,4);
mysql> select * from student where id=2 union all select * from student where id=4;
image.png

子查询

mysql> select user_name, id from student where id in (select stu_id from mark);

# 结果
+-----------+----+
| user_name | id |
+-----------+----+
| jim       |  2 |
| Tom       |  3 |
| Rose      |  4 |
| zhuliye   |  5 |
| John      |  6 |
+-----------+----+
5 rows in set (0.00 sec)
image.png

限制查询条数limit

limit的作用主要是有两个:

  • 如果带上一个参数,则限制每次显示的记录条数
  • 如果带上两个参数,则第一个表示从哪个位置开始显示,第二个表示显示多少条记录,可以当做“分页功能”
  • student表为栗子:

若带上两个参数

image.png

带上两个参数形式等价于limit...offset...

image.png

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,108评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,699评论 1 296
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,812评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,236评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,583评论 3 288
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,739评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,957评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,704评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,447评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,643评论 2 249
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,133评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,486评论 3 256
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,151评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,108评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,889评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,782评论 2 277
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,681评论 2 272

推荐阅读更多精彩内容