第六章 DQL 資料查詢語言
阿新 • • 發佈:2020-10-26
一、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結尾的校驗規則區分大小寫