第六章 DQL 数据查询语言
一、select 简单查询命令
#1.查询表中所有的数据
mysql> select * from test.student;
#2.查看所有数据之前,先查看数据量
mysql> select count(*) from test.student;
#3.查询指定列
mysql> select user,host from mysql.user;
#4.按条件查询
mysql> select * from test.student where id='8';
mysql> select id,name from test.student where id='8';
2.查询数据测试
1)将sql导入数据库
#上传sql文件到服务器
[root@db01 ~]# rz world.sql
#导入sql到数据库
mysql> source /root/world.sql;
mysql> \. /root/world.sql
2)查询的操作
#1.查看库下面的表
mysql> show tables from world;
mysql> use world
mysql> show tables;
+-----------------+
| Tables_in_world |
+-----------------+
| city |
| country |
| countrylanguage |
+-----------------+
3 rows in set (0.00 sec)
#2.查看表结构
mysql> desc city;
#3.查询所有数据
mysql> select count(*) from city;
mysql> select * from city;
#4.查询指定列数据
mysql> select name,population from city;
#5.按照人口数量排序
#升序
mysql> select name,population from city order by population;
#降序
mysql> select name,population from city order by population desc;
#6.查看人口数量最多排名前十的城市
mysql> select name,population from city order by population desc limit 10;
#7.按照步长查询数据
#查询数据从10后面开始计算,展示20条数据,20就是步长
mysql> select id,name,population from city limit 10,20;
mysql> select id,name,population from city limit 0,60;
mysql> select id,name,population from city limit 60,60;
mysql> select id,name,population from city limit 120,60;
3.按条件查询
#1.条件查询where的符号
where的条件符号: = < > >= <= != <>
where的连接符:and or like in
#2.查看中国城市的人口数量
mysql> select CountryCode,name,population from city where CountryCode='CHN';
#3.查看黑龙江省城市的人口数量
mysql> select CountryCode,District,name,population from city where CountryCode='CHN' and District='heilongjiang';
#4.查询中国人口数量小于10万的城市
mysql> select CountryCode,population,name from city where CountryCode='CHN' and population<'100000';
#5.查看国家代码以H开头的
mysql> select * from city where CountryCode like 'H%';
#6.查看国家代码以H结尾的
mysql> select * from city where CountryCode like '%H';
#7.查看国家代码包含H的
mysql> select * from city where CountryCode like '%H%';
#8.查询中国城市和美国城市的人口数量
mysql> select CountryCode,name,population from city where CountryCode='CHN' or CountryCode='USA';
mysql> select CountryCode,name,population from city where CountryCode in ('CHN','USA');
#9.联合查询
mysql> select CountryCode,name,population from city where CountryCode='CHN' union all select CountryCode,name,population from city where CountryCode='USA';
二、select 高级用法(多表联查,连表查询)
1.传统连接
1)数据
[qiudao,zengdao,qiandao]
[80,90,100]
#建表
id:[1,2,3]
name:[qiudao,zengdao,qiandao]
#建表
id:[1,2,3]
mark:[80,90,100]
2)建表
#建立学生表
mysql> create table student1(id int,name varchar(20));
#建立成绩表
mysql> create table score(id int,mark int);
3)插入数据
#插入学生表数据
mysql> insert student1 values('1','qiudao'),('2','zengdao'),('3','qiandao');
#插入成绩表数据
mysql> insert score values('1','80'),('2','90'),('3','100');
4)查看数据
#查看学生表
mysql> select * from student1;
+------+---------+
| id | name |
+------+---------+
| 1 | qiudao |
| 2 | zengdao |
| 3 | qiandao |
+------+---------+
3 rows in set (0.00 sec)
#查看成绩表
mysql> select * from score;
+------+------+
| id | mark |
+------+------+
| 1 | 80 |
| 2 | 90 |
| 3 | 100 |
+------+------+
3 rows in set (0.00 sec)
5)数据查询
#查看qiudao的成绩
1.方式一:
mysql> select student1.name,score.mark from student1,score where student1.id='1' and score.id='1';
2.方式二:
mysql> select student1.name,score.mark from student1,score where student1.id=score.id and name='qiudao';
6)查询题1:
#查询世界上小于100人的城市是哪个国家的?
#1.审题:查看需要查询哪些数据
城市名字 城市人口数量 国家名字
#2.找到查询内容的字段在哪个表
城市名字 城市人口数量 国家名字
city.name city.population country.name
#3.找出两个表中关联的列
city.countrycode
country.code
#4.编写语句
select city.name,city.population,country.name from city,country where city.countrycode=country.code and city.population < '100';
select city.name,city.population,country.name from city natural join country where city.population < '100';
7)多表联查练习题2:
#查询世界上小于100人的城市是哪个国家的,使用什么语言?
#1.审题:查看需要查询哪些数据
城市名字 城市人口数量 国家名字 国家的语言
#2.找到查询内容的字段在哪个表
城市名字 城市人口数量 国家名字 国家的语言
city.name city.population country.name countrylanguage.language
#3.找出三个表相关联的列
city.countrycode
country.code
countrylanguage.CountryCode
#4.编写语句
select city.name,city.population,country.name,countrylanguage.language from city,country,countrylanguage where city.countrycode=country.code and country.code=countrylanguage.CountryCode and city.population < '100';
2.自连接
#自连接会自动关联两个表中数据相同的字段,自连接的两个表必须有相同的字段和数据
1)自连接查询
#查询人口数量大于100万的城市,列出他们的国家代码和国家语言
1.传统连接:
select city.name,city.population,countrylanguage.CountryCode,countrylanguage.language from city,countrylanguage where countrylanguage.CountryCode=city.CountryCode and city.population > '1000000';
2.自连接:
select city.name,city.population,countrylanguage.CountryCode,countrylanguage.language from city natural join countrylanguage where city.population > '1000000';
#注意:
1.自连接会自动去获取两个表之间的关联列和数据,所以自连接的两个表必须有相同的字段和数据
3.内连接
1)语法
select * from 表1 join 表2 on 关联条件 where 条件
#注意:
表 1 是小表
表 2 是大表
2)例子:
#查询世界上小于100人的城市是哪个国家的,国家代码是什么
1.传统链接:
select city.population,city.name,country.name,country.code from city,country where country.code=city.countrycode and city.population < '100';
2.内连接:
select city.population,city.name,country.name,country.code from country join city on country.code=city.countrycode where city.population < '100';
3)内连接三表联查
#查询世界上小于100人的城市是哪个国家的,用什么语言?
select city.population,city.name,country.name,countrylanguage.language from country join city on city.countrycode=country.code join countrylanguage on country.code=countrylanguage.countrycode where city.population < '100';
4.外连接
1)左外连接
select city.name,city.countrycode,country.name
from city left join country
on city.countrycode=country.code
and city.population<100;
2)右外连接
select city.name,city.countrycode,country.name
from city right join country
on city.countrycode=country.code
and city.population<100;
5.UNION(合并查询)
#范围查询OR语句
mysql> select * from city where countrycode='CHN' or countrycode='USA';
#范围查询IN语句
mysql> select * from city where countrycode in ('CHN','USA');
#替换为:
mysql> select * from city where countrycode='CHN'
union all
select * from city where countrycode='USA' limit 10;
三、字符集
1.字符集介绍
字符集:是一个系统支持的所有抽象字符的集合。字符是各种文字和符号的总称,包括各国家文字、标点符号、图形符号、数字等
#最早的字符集:ASCII码
中国的字符集:gbk,utf8,utf8mb4,gbk2312,....
日本:shift-JIS
韩国:Euc-kr
万国编码:Unicode字符集
#数据库常用的字符集
gbk: 一个汉字占用2个字节
utf8: 一个汉字占用3个字节
utf8mb4: 一个汉字占用4个字节
#字符集修改
字符集有一个包含关系,修改时要注意小的范围可以修改为大范围的字符集
#数据库查看字符集
mysql> show charset;
2.校验规则
#查看校验规则
mysql> show collation;
| latin1_bin |
| latin1_general_ci |
| latin1_general_cs |
#校验规则区别
1.ci结尾的校验规则不区分大小写
2.bin和cs结尾的校验规则区分大小写
第六章 DQL 数据查询语言的更多相关文章
- 八:SQL之DQL数据查询语言单表操作
前言: DQL数据库查询语言是我们在开发中最常使用的SQL,这一章总结了单表操作部分的常用查询方式 主要操作有:查询所有字段.查询指定字段.查询指定记录.带IN的关键字查询,范围查询,陪查询.查询空值 ...
- 第六章 大数据,6.3 突破传统,4k大屏的沉浸式体验(作者: 彦川、小丛)
6.3 突破传统,4k大屏的沉浸式体验 前言 能够在 4K 的页面上表演,对设计师和前端开发来说,即是机会也是挑战,我们可以有更大的空间设计宏观的场景,炫酷的转场,让观众感受影院式视觉体验,但是,又必 ...
- apue学习笔记(第六章 系统数据文件和信息)
UNIX系统的正常运作需要使用大量与系统有关的数据文件,例如,口令文件/etc/passwd和组文件/etc/group就是经常被多个程序频繁使用的两个文件. 口令文件 UNIX系统口令文件包含如下字 ...
- 九:SQL之DQL数据查询语言多表操作
前言: 一:数据准备 员工表emp 和部门表 dept 注意:我在录入员工表的时候,特意添加了两条没有部门的员工,他们的部门id对应为null; --分别创建部门和员工表,并实现一对多关系 DROP ...
- UNIX系统高级编程——第六章-系统数据文件和信息-总结
口令文件: /* The passwd structure. */ struct passwd { char *pw_name; /* Username. */ char *pw_passwd; /* ...
- DQL 数据查询语言 IS (information_schema)
3.information_schema 统计信息库 1.介绍: 视图 1.安全: 只允许查询,不知道操作的对象是谁. 2.方便: 只需要简单的select语句即可使用. 2.作用: 1.方便我们做数 ...
- DQL 数据查询语言 select
1.select 1.select 单独使用 (1) 查询数据库的参数 查看端口: select @@port; 查看数据路径 select @@datadir; (2)调用内置函数 查看当前库 se ...
- MySQL数据库之DQL(数据查询语言)
1.MySQL之DQL查询AS CONCAT LIKE的使用 (1)select 列名1,列名2,...... from 表名 [where 条件] 查询所有字段用*,不带where条件的话,就会把表 ...
- DQL 数据查询语言
查询数据(SELECT) # 查询所有数据 - 很危险,数据量过大,容易导致内存溢出而宕机 mysql> select * from student; # 先查询数据总量,然后决定是否可以查询所 ...
随机推荐
- Unity 自己使用顶点描绘圆形UI图片
2020-09-10 在游戏的UI中,圆形图片的需求是很高的,但是,在Unity中想要实现圆形UI,一般的做法是是使用圆形Mask(遮罩),但是使用Mask的缺点很明显,主要有三点: 1.比较麻烦,使 ...
- Spring--AOP的见解
AOP是指面向切面编程,与JAVA中的动态代理有很深的渊源. 在使用Spring框架时,AOP编程能简化很多繁杂的步骤,精简代码. 切面:横切关注点(跨越程序中多个模块的功能),被模块化的特殊对象,也 ...
- git的详细使用,项目创建到同步远程仓库,版本回退,忽略文件,分支创建,分支合并,分支名称修改,冲突解决,项目迁移
注意:此处省略git的安装 1..git的工作流程示意图: 2.本地仓库的初始化: 2.1 创建一个文件夹,如我创建的是:D:\gitdemo\shop 2.2 进入shop目录,鼠标右键,打开git ...
- Word rings
Word rings 题目描述 这道题就是想求出所有的环,然后在所有环中比较出环串的平均长度最长的那一个,然后就输出平均长度最长的,如果在一个点当中的样例中没有环的话我们就应该输出"No S ...
- 【题解】SAC E#1 - 一道难题 Tree
Problem is here \(\text{Solution:}\) 首先,一眼看出这是最小割,只要叶子节点对汇点\(T\)连接流量为\(inf\)的边就可以一遍最大流搞定了. 剩下的问题在于,如 ...
- 带着好奇心去探索IDEA
带着好奇心去探索IDEA 工欲善其事必先利其器 软件是提高工作效率的工具.所以了解工具的特性,操作方式,能更好地使用它.一般使用掌握逻辑: 第一步:了解菜单栏-工具栏-其他窗口: 第二步:实战,真正利 ...
- ubuntu20 使用命令安装 redis
安装 redis sudo apt-get install redis-server -y 配置文件 vi /etc/redis/redis.conf # 设置端口 port # 设置密码 requi ...
- 关于IPA文件重签名后如何跟踪管理APP的技术探讨和实践演示
前言:开发iOS的朋友都知道,在功能开发完成后,我们就会用申请的苹果账号在后台做证书配置,然后提交到AppStore,但是也有部分APP我们不需要提交到AppStore,比如内部测试用的APP.定制给 ...
- ie 版本判断脚本
// 获取IE版本 /** * @return {string} */ function IEVersion() { // 取得浏览器的userAgent字符串 var userAgent = nav ...
- Springboot集成JUnit5优雅进行单元测试
为什么使用JUnit5 JUnit4被广泛使用,但是许多场景下使用起来语法较为繁琐,JUnit5中支持lambda表达式,语法简单且代码不冗余. JUnit5易扩展,包容性强,可以接入其他的测试引擎. ...