日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > 数据库 >内容正文

数据库

mysql useing查询,MySQL数据库之多表查询using优化与案例

發(fā)布時(shí)間:2024/4/19 数据库 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql useing查询,MySQL数据库之多表查询using优化与案例 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。