mysql useing查询,MySQL数据库之多表查询using优化与案例
using
概念
using用來(lái)指定連接字段
using的結(jié)果也會(huì)對(duì)公共字段進(jìn)行優(yōu)化,優(yōu)化的規(guī)則和自然連接是一樣的
MariaDB [sel]> select * from grades inner join resume using(name);
+-------+---------+------+----+-----------+
| name | chinese | math | id | skill |
+-------+---------+------+----+-----------+
| Sunny | 93 | 96 | 1 | php |
| Jerry | 97 | 91 | 3 | php,mysql |
+-------+---------+------+----+-----------+
# `2 rows in set (0.001 sec)`
MySQL練習(xí)題
顯示地區(qū)及每個(gè)地區(qū)參加筆試的人數(shù),并按人數(shù)降序排列
思路分解
select 查詢字段
from 多表查詢 左外連接
using 指定連接字段
group by 分組查詢結(jié)果
order by 降序排列
-- 第一步: 顯示地區(qū)及每個(gè)地區(qū)參加筆試的人數(shù)
mysql> select stuaddress,count(writtenexam) from stuinfo left join stumarks using(stuno) group by stuaddress;
+------------+--------------------+
| stuaddress | count(writtenexam) |
+------------+--------------------+
| 上海 | 1 |
| 北京 | 2 |
| 天津 | 2 |
| 河北 | 0 |
| 河南 | 0 |
+------------+--------------------+
# `5 rows in set (0.00 sec)`
-- 第二步:將結(jié)果降序排列
mysql> select stuaddress,count(writtenexam) c from stuinfo left join stumarks using(stuno) group by stuaddress order by c desc;
+------------+---+
| stuaddress | c |
+------------+---+
| 北京 | 2 |
| 天津 | 2 |
| 上海 | 1 |
| 河北 | 0 |
| 河南 | 0 |
+------------+---+
# `5 rows in set (0.00 sec)`
顯示有學(xué)生參加考試的地區(qū)
思路解析
select 選擇查詢字段
from 多表查詢 左外連接
using 指定連接字段
group by 分組查詢顯示
having 條件篩選
-- having篩選
mysql> select stuaddress,count(writtenexam) c from stuinfo left join stumarks using(stuno) group by stuaddress having c>0;
+------------+---+
| stuaddress | c |
+------------+---+
| 上海 | 1 |
| 北京 | 2 |
| 天津 | 2 |
+------------+---+
# `3 rows in set (0.00 sec)`
思路解析
select 選擇查詢字段
from 多表查詢 右外連接
using 指定連接字段
distinct 去重復(fù)
having 條件篩選
is not null 去空
-- 表連接實(shí)現(xiàn)
-- 第一步:右連接獲取有成績(jī)的地區(qū)
mysql> select stuaddress from stuinfo right join stumarks using(stuno);
+------------+
| stuaddress |
+------------+
| 北京 |
| 上海 |
| 天津 |
| 北京 |
| 天津 |
| NULL |
+------------+
# `6 rows in set (0.00 sec)`
-- 第二步:去重復(fù)
mysql> select distinct stuaddress from stuinfo right join stumarks using(stuno);
+------------+
| stuaddress |
+------------+
| 北京 |
| 上海 |
| 天津 |
| NULL |
+------------+
# `4 rows in set (0.00 sec)`
-- 去除null
mysql> select distinct stuaddress from stuinfo right join stumarks using(stuno) having stuaddress is not null;
+------------+
| stuaddress |
+------------+
| 北京 |
| 上海 |
| 天津 |
+------------+
# `3 rows in set (0.00 sec)`
顯示男生和女生的人數(shù)
方法一:分組查詢
select 查詢字段
from 查詢表
group by 分組查詢顯示
mysql> select stusex,count(*) from stuinfo group by stusex;
+--------+----------+
| stusex | count(*) |
+--------+----------+
| 女 | 3 |
| 男 | 4 |
+--------+----------+
# `2 rows in set (0.00 sec)`
方法二:union
select 查詢字段
from 查詢表
where 條件篩選
union 聯(lián)合查詢
mysql> select stusex,count(*) from stuinfo where stusex='男' union select stusex,count(*) from stuinfo where stusex='女';
+--------+----------+
| stusex | count(*) |
+--------+----------+
| 男 | 4 |
| 女 | 3 |
+--------+----------+
# `2 rows in set (0.00 sec)`
方法三:直接寫條件
select 聚合函數(shù)查詢
from 查詢表
mysql> select sum(stusex='男') 男,sum(stusex='女') 女 from stuinfo;
+------+------+
| 男 | 女 |
+------+------+
| 4 | 3 |
+------+------+
# `1 row in set (0.00 sec)`
顯示每個(gè)地區(qū)男生、女生、總?cè)藬?shù)
思路解析
select 選擇字段 聚合函數(shù)
from 選擇表
group by 分組查詢顯示
mysql> select stuaddress,count(*) 總?cè)藬?shù),sum(stusex='男') 男,sum(stusex='女') 女 from stuinfo group by stuaddress;
+------------+--------+------+------+
| stuaddress | 總?cè)藬?shù) | 男 | 女 |
+------------+--------+------+------+
| 上海 | 1 | 1 | 0 |
| 北京 | 2 | 1 | 1 |
| 天津 | 2 | 2 | 0 |
| 河北 | 1 | 0 | 1 |
| 河南 | 1 | 0 | 1 |
+------------+--------+------+------+
# `5 rows in set (0.00 sec)`
標(biāo)簽:stusex,mysql,查詢,stuaddress,MySQL,using,之多表,select
來(lái)源: https://www.cnblogs.com/SharkJiao/p/14137851.html
總結(jié)
以上是生活随笔為你收集整理的mysql useing查询,MySQL数据库之多表查询using优化与案例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 上拉加载 php,php+jquery
- 下一篇: 安装mysql 5.6.24给linux