在mysql怎样查询地址和电话_Mysql数据查询
Mysql查詢
數(shù)據(jù)多次過濾
條件:from、where、group by、having、distinct、order by、limit => 層層篩選后的結(jié)果
查:
select [distinct] 字段1 [[as] 別名1],...,字段n [[as] 別名n] from [數(shù)據(jù)庫名.]表名 [條件];
注:一條查詢語句,可以擁有多種篩選條件,條件的順序必須按照上方順序進行逐步篩選,distinct稍有特殊(書寫位置),條件的種類可以不全
可以缺失,但不能亂序
單表查詢
distinct 去重
數(shù)據(jù)為:
+------+------+
| x | y |
+------+------+
| 1 | 2 |
| 2 | 3 |
| 3 | 4 |
| 1 | 2 |
+------+------+
#執(zhí)行
select distinct * from t1;
+------+------+
| x | y |
+------+------+
| 1 | 2 |
| 2 | 3 |
| 3 | 4 |
+------+------+
# 總結(jié):distinct對參與查詢的所有字段,整體去重(所查的全部字段的值都相同,才認為是重復(fù)數(shù)據(jù))
常用函數(shù)
拼接:concat() | concat_ws()
大小寫:upper() | lower()
浮點型操作:ceil() | floor() | round()
整型:可以直接運算
concat()和concat_ws()
mysql>: select name as 姓名, concat(area,'-',port) 地址 from emp; # 上海-浦東
mysql>: select name as 姓名, concat_ws('-',area,port,dep) 信息 from emp; # 上海-浦東-教職部
#concat_ws(),可以在每個字段之間加上第一個字符
upper()和lower()
select upper(name) 姓名大寫,lower(name) 姓名小寫 from emp;
ceil(),floor(),round()
#元數(shù)據(jù)
+------+
| x |
+------+
| 1.5 |
| 1.2 |
| 1.51 |
+------+
#ceil() 向上取整
mysql> select ceil(x) from t2;
+---------+
| ceil(x) |
+---------+
| 2 |
| 2 |
| 2 |
+---------+
#floor()向下取整
mysql> select floor(x) from t2;
+----------+
| floor(x) |
+----------+
| 1 |
| 1 |
| 1 |
+----------+
#round() 特殊版本的四舍五入 5的時候舍
mysql> select round(x) from t2;
+----------+
| round(x) |
+----------+
| 2 |
| 1 |
| 2 |
+----------+
整數(shù)
select x+1 from t1;
where
常用判斷
比較符合:> | < | >= | <= | = | !=
區(qū)間符合:between 開始 and 結(jié)束 | in(自定義容器)| not in
邏輯符合:and | or | not
相似符合:like _|%
正則符合:regexp 正則語法
自定義區(qū)間:id in (2,3) | id not in (2,3) | id < all(2,3) | id < any(2,3)
案例
mysql>: select * from emp where salary>5;
mysql>: select * from emp where id%2=0;
mysql>: select * from emp where salary between 6 and 9;
mysql>: select * from emp where id in(1, 3, 7, 20);
# _o 某o | __o 某某o | _o% 某o* (*是0~n個任意字符) | %o% *o*
mysql>: select * from emp where name like '%o%';
mysql>: select * from emp where name like '_o%';
mysql>: select * from emp where name like '___o%';
# sql只支持部分正則語法
mysql>: select * from emp where name regexp '.*\d'; # 不支持\d代表數(shù)字,認為\d就是普通字符串
mysql>: select * from emp where name regexp '.*[0-9]'; # 支持[]語法
all與any:區(qū)間修飾條件
# 語法規(guī)則
# where id in (1, 2, 3) => id是1或2或3
# where id not in (1, 2, 3) => id不是1,2,3
# where salary < all(3, 6, 9) => salary必須小于所有情況(小于最小)
# where salary > all(3, 6, 9) => salary必須大于所有情況(大于最大)
# where salary < any(3, 6, 9) => salary只要小于一種情況(小于最大)
# where salary > any(3, 6, 9) => salary只要大于一種情況(大于最小)
in < > ()
# 案例
select * from emp where salary < all(select salary from emp where id>11);
分組group by
分組與篩選:group by|having
where和having區(qū)別
1.where在分組之前限定,如果不滿足條件,則不參與分組。
having在分組之后進行限定,如果不滿足結(jié)果,則不會被查詢出來
2.where后不可以跟聚合函數(shù),having可以進行聚合函數(shù)的判斷
聚合函數(shù)
max():最大值
min():最小值
avg():平均值
sum():和
count():記數(shù)
group_concat():組內(nèi)字段拼接,用來查看組內(nèi)其他字段
分組查詢group by
配置
# 修改my.ini配置重啟mysql服務(wù)
sql_mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
注意
分組查詢的字段只有:分組字段、聚合函數(shù)
having不能使用聚合函數(shù)別名
完整體
select sex,AVG(math),COUNT(id) from student where math>70 group by sex having count(id) > 2
實驗
#元數(shù)據(jù)
+------+------+
| z1 | z2 |
+------+------+
| 1 | zx |
| 2 | zy |
| 9 | zx |
| 5 | zy |
| 6 | zx |
| 7 | zy |
+------+------+
#執(zhí)行
select z2,avg(z1) from t3 group by z2;
+------+---------+
| z2 | avg(z1) |
+------+---------+
| zx | 5.3333 |
| zy | 4.6667 |
+------+---------+
聯(lián)合分組
簡單來說就是對多個字段分組
#執(zhí)行
select z1,z2 from t3 group by z1,z2;
+------+------+
| z1 | z2 |
+------+------+
| 1 | zx |
| 2 | zy |
| 5 | zy |
| 6 | zx |
| 7 | zy |
| 9 | zx |
+------+------+
排序
排序規(guī)則
# order by 主排序字段 [asc|desc], 次排序字段1 [asc|desc], ...次排序字段n [asc|desc]
未分組狀態(tài)下
mysql>: select * from emp;
# 按年齡升序
mysql>: select * from emp order by age asc;
# 按薪資降序
mysql>: select * from emp order by salary desc;
# 按薪資降序,如果相同,再按年齡降序
mysql>: select * from emp order by salary desc, age desc;
# 按齡降序,如果相同,再按薪資降序
mysql>: select * from emp order by age desc, salary desc;
分組狀態(tài)下
mysql>:
select
dep 部門,
group_concat(name) 成員,
max(salary) 最高薪資,
min(salary) 最低薪資,
avg(salary) 平均薪資,
sum(salary) 總薪資,
count(gender) 人數(shù)
from emp group by dep;
# 最高薪資降序
mysql:
select
dep 部門,
group_concat(name) 成員,
max(salary) 最高薪資,
min(salary) 最低薪資,
avg(salary) 平均薪資,
sum(salary) 總薪資,
count(gender) 人數(shù)
from emp group by dep
order by 最高薪資 desc;
分頁limit
# 語法:limit 條數(shù) | limit 偏移量,條數(shù)
#元數(shù)據(jù)
+------+------+
| z1 | z2 |
+------+------+
| 1 | zx |
| 2 | zy |
| 9 | zx |
| 5 | zy |
| 6 | zx |
| 7 | zy |
+------+------+
#執(zhí)行
select * from t3 limit 1;
+------+------+
| z1 | z2 |
+------+------+
| 1 | zx |
+------+------+
#執(zhí)行
select * from t3 limit 2,2;
+------+------+
| z1 | z2 |
+------+------+
| 9 | zx |
| 5 | zy |
+------+------+
多表查詢
分類
1.內(nèi)連接
2.左連接
3.右連接
4.全連接
from 左表 inner|left|right join 右表 on 兩個表之間的關(guān)聯(lián)條件
inner:都是有用的
left:左邊全有
right:右邊全有
全連:left union right 有用沒用都有
笛卡爾積
笛卡爾積是數(shù)據(jù)庫多表連接的基礎(chǔ),它會列出所有的組合,多表查詢其實就是對笛卡爾積進行過濾留下有用的數(shù)據(jù)
1 2 3和z x y的笛卡爾積
1 z 1 x 1 y
2 z 2 x 2 y
3 z 3 x 3 y
select * from z1,z2
一對一和一對多
元數(shù)據(jù)
+------+----------+
| id | name |
+------+----------+
| 1 | 教室_1 |
| 2 | 教室_2 |
| 3 | 教室_3 |
| 4 | NULL |
| NULL | NULL |
+------+----------+
+------+------+
| id | name |
+------+------+
| 1 | zx |
| 2 | wl |
| 3 | zy |
| 1 | zxy |
| 2 | fd |
| 3 | hj |
| 4 | ds |
| 1 | NULL |
| NULL | NULL |
+------+------+
內(nèi)連接
找兩張表共有的部分,相當于利用條件從笛卡爾積結(jié)果中篩選出了正確的結(jié)果
# 關(guān)鍵字:inner join on
# 語法:from A表 inner join B表 on A表.關(guān)聯(lián)字段=B表.關(guān)聯(lián)字段
可以簡寫為join
#執(zhí)行
select * from class join student on class.id=student.id;
+------+----------+------+------+
| id | name | id | name |
+------+----------+------+------+
| 1 | 教室_1 | 1 | zx |
| 2 | 教室_2 | 2 | wl |
| 3 | 教室_3 | 3 | zy |
| 1 | 教室_1 | 1 | zxy |
| 2 | 教室_2 | 2 | fd |
| 3 | 教室_3 | 3 | hj |
| 4 | NULL | 4 | ds |
| 1 | 教室_1 | 1 | NULL |
+------+----------+------+------+
# 總結(jié):只保留兩個表有關(guān)聯(lián)的數(shù)據(jù)
左連接
優(yōu)先顯示左表全部記錄
# 關(guān)鍵字:left join on
# 語法:from 左表 left join 右表 on 左表.關(guān)聯(lián)字段=右表.關(guān)聯(lián)字段
#執(zhí)行
select class.id,class.name,student.name from class left join student on class.id=student.id;
+------+----------+------+
| id | name | name |
+------+----------+------+
| 1 | 教室_1 | zx |
| 2 | 教室_2 | wl |
| 3 | 教室_3 | zy |
| 1 | 教室_1 | zxy |
| 2 | 教室_2 | fd |
| 3 | 教室_3 | hj |
| 4 | NULL | ds |
| 1 | 教室_1 | NULL |
| NULL | NULL | NULL |
+------+----------+------+
# 總結(jié):保留左表的全部數(shù)據(jù),右表有對應(yīng)數(shù)據(jù)直接連表顯示,沒有對應(yīng)關(guān)系空填充
右連接
優(yōu)先顯示右表全部記錄
# 關(guān)鍵字:right join on
# 語法:from A表 right join B表 on A表.關(guān)聯(lián)字段=B表關(guān)聯(lián)字段
#執(zhí)行
select class.id,student.name,class.name from class right join student on class.id=student.id;
+------+------+----------+
| id | name | name |
+------+------+----------+
| 1 | zx | 教室_1 |
| 1 | zxy | 教室_1 |
| 1 | NULL | 教室_1 |
| 2 | wl | 教室_2 |
| 2 | fd | 教室_2 |
| 3 | zy | 教室_3 |
| 3 | hj | 教室_3 |
| 4 | ds | NULL |
| NULL | NULL | NULL |
+------+------+----------+
# 總結(jié):保留右表的全部數(shù)據(jù),左表有對應(yīng)數(shù)據(jù)直接連表顯示,沒有對應(yīng)關(guān)系空填充
左右可以相互轉(zhuǎn)化
更換一下左右表的位置,相對應(yīng)更換左右連接關(guān)鍵字,結(jié)果相同
全連接
全外連接:在內(nèi)連接的基礎(chǔ)上增加左邊有右邊沒有的和右邊有左邊沒有的結(jié)果
#執(zhí)行
select class.id,student.name,class.name from class left join student on class.id=student.id
UNION
select class.id,student.name,class.name from class right join student on class.id=student.id
order by id;
+------+------+----------+
| id | name | name |
+------+------+----------+
| NULL | NULL | NULL |
| 1 | zxy | 教室_1 |
| 1 | NULL | 教室_1 |
| 1 | zx | 教室_1 |
| 2 | fd | 教室_2 |
| 2 | wl | 教室_2 |
| 3 | hj | 教室_3 |
| 3 | zy | 教室_3 |
| 4 | ds | NULL |
+------+------+----------+
# 總結(jié):左表右表數(shù)據(jù)都被保留,彼此有對應(yīng)關(guān)系正常顯示,彼此沒有對應(yīng)關(guān)系均空填充對方
# 注意 union與union all的區(qū)別:union會去掉相同的紀錄
多對多
相當于在一對多的基礎(chǔ)上,多建立一次連接
##源數(shù)據(jù)
#人物表
zx
+----+------+
| id | name |
+----+------+
| 1 | zx |
| 2 | wl |
| 3 | zy |
| 4 | zxy |
+----+------+
#關(guān)系表
zx_zy
+----+-------+-------+
| id | zx_id | zy_id |
+----+-------+-------+
| 1 | 1 | 1 |
| 2 | 1 | 3 |
| 3 | 2 | 2 |
| 4 | 2 | 4 |
| 5 | 3 | 1 |
| 6 | 3 | 3 |
| 7 | 4 | 5 |
| 8 | 4 | 2 |
| 9 | 4 | 3 |
+----+-------+-------+
#技能表
zy
+----+-----------+
| id | name |
+----+-----------+
| 1 | 吃飯 |
| 2 | 睡覺 |
| 3 | 打代碼 |
| 4 | 寫作業(yè) |
| 5 | 上廁所 |
+----+-----------+
查詢1
#執(zhí)行
以關(guān)系表為中心,進行兩次左連接
select zx_zy.id,zx.name,zy.name from zx_zy LEFT JOIN zx on zx.id=zx_zy.zx_id LEFT JOIN zy on zy.id=zx_zy.zy_id
+----+------+-----------+
| id | name | name |
+----+------+-----------+
| 1 | zx | 吃飯 |
| 2 | zx | 打代碼 |
| 3 | wl | 睡覺 |
| 4 | wl | 寫作業(yè) |
| 5 | zy | 吃飯 |
| 6 | zy | 打代碼 |
| 7 | zxy | 上廁所 |
| 8 | zxy | 睡覺 |
| 9 | zxy | 打代碼 |
+----+------+-----------+
查詢2
#執(zhí)行
任意表,進行兩次內(nèi)連接
select zx_zy.id,zx.name,zy.name from zx join zx_zy on zx_zy.zx_id=zx.id join zy on zx_zy.zy_id=zy.id
+----+------+-----------+
| id | name | name |
+----+------+-----------+
| 1 | zx | 吃飯 |
| 2 | zx | 打代碼 |
| 3 | wl | 睡覺 |
| 4 | wl | 寫作業(yè) |
| 5 | zy | 吃飯 |
| 6 | zy | 打代碼 |
| 7 | zxy | 上廁所 |
| 8 | zxy | 睡覺 |
| 9 | zxy | 打代碼 |
+----+------+-----------+
子查詢
簡單來說就是一個查詢的數(shù)據(jù)當做另一個查詢的查詢條件
要注意的是子查詢的數(shù)據(jù)一般有好多,一般用in
# 增:insert into 表 select子查詢
# 刪:delete from 表 條件是select子查詢(表不能與delete表相同)
# 查:select 字段 from 表 條件是select子查詢
# 改:update 表 set 字段=值 條件是select子查詢(表不能與update表相同)
簡單案例-子查詢
##元數(shù)據(jù)
t1
+------+------+
| x | y |
+------+------+
| 1 | 2 |
| 2 | 3 |
| 3 | 4 |
| 1 | 2 |
+------+------+
t3
+------+------+
| z1 | z2 |
+------+------+
| 1 | zx |
| 2 | zy |
| 9 | zx |
| 5 | zy |
| 6 | zx |
| 7 | zy |
+------+------+
##需求根據(jù)t1的x,刪除t3,與t1相同的z1數(shù)據(jù)
#執(zhí)行
select * from t3;
+------+------+
| z1 | z2 |
+------+------+
| 9 | zx |
| 5 | zy |
| 6 | zx |
| 7 | zy |
+------+------+
總結(jié)
以上是生活随笔為你收集整理的在mysql怎样查询地址和电话_Mysql数据查询的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux一个命令创建多个目录:seq命
- 下一篇: Linux项目第一次访问非常慢,后来很快