mysql 语句检查_mysql查询语句
一、簡(jiǎn)單查詢
1.最簡(jiǎn)單查詢(查所有數(shù)據(jù))
select * from 表名 ? 注意:* 代表所有列,并不是代表所有行
例:select * from test
2.查詢指定列
select 列名,列名 from 表名
例:select code,name from test
3.修改結(jié)果集的列名 as
select 列名 as '顯示的字' from 表名
例:select code as '代號(hào)',name as '姓名' from test
4.條件查詢
select * from 表名 where 條件
例:select * from test where code='n003'
5.多條件查詢
或者 or:select * from 表名 where 條件 or 條件
例:select * from test where code='p003' or nation='n001'
并且 and:select * from 表名 where 條件 and 條件
例:select * from test where code='p004' and nation='n001'
6.范圍查詢 (某一列的內(nèi)容是誰(shuí)到誰(shuí)之間的數(shù)據(jù))
例:兩種寫法:查找汽車價(jià)格在40到60之間
(1)select * from car where price>=40 and price>=60
(2)select * from car where price between 40 and 60
7.離散查詢
查詢汽車價(jià)格在(10、20、30、40、50、60)中出現(xiàn)的信息 in
例:兩種寫法
(1)select * from car where price=10 or price=20 or price=30 or price=40 or price=50 or price=60
(2)select * from car where price in(10,20,30,40,50,60)
不在(10、20、30、40、50、60)中出現(xiàn)的信息 not in
例:select * from car where price not in(10,20,30,40,50,60)
8.模糊查詢(關(guān)鍵字查詢)like
%:任意n個(gè)字符
_:任意一個(gè)字符
查詢汽車表名稱中包含奧迪
例:select * from car where namelike '%奧迪%'
查詢汽車表名稱第二個(gè)字符為“馬”的汽車
例:select * from car where name like '_馬%'
9.排序查詢 order by
升序 asc,可省略
例:汽車表中價(jià)格列升序
select * from car order by price asc
降序 desc(從高到低)
例:汽車表中油耗列降序
select * from car order by oil desc
先a列升序后b列降序
例:汽車表中先將a列升序后將b列降序
select * from car order by a,b desc
10.去重查詢 distinct
例:查找汽車表中型號(hào)一樣的去重
select distinct brand from car
11.分頁(yè)查詢
一頁(yè)顯示m條數(shù)據(jù) 當(dāng)前是第n頁(yè)
limit (n-1)*m,m
一頁(yè)顯示10條數(shù)據(jù) 當(dāng)前是第二頁(yè) 跳過多少條,取多少條
例:select * from chinastates limit 10,10
12.聚合函數(shù)(統(tǒng)計(jì)函數(shù))
(1)總數(shù) count(*):查詢數(shù)據(jù)總條數(shù)
例:select count(*) from chinastates
count(主鍵列 areacode)
例:select count(areacode) from chinastates
(2)求和 sum(求價(jià)格和列)
例:select sum(price) from car
(3)求平均 avg(求價(jià)格平均列)
例:select avg(price) from car
(4)取最大值、最小值(價(jià)格列)
例:
select max(price) from car
select min(price) from car
13.分組查詢 group by
查詢汽車表中每個(gè)系列下有多少汽車
例:select brand,count(*) from car group by brand
查詢汽車表中賣的汽車數(shù)量大于3的系列 ? ?注意: ?group by....having(條件)
例:select brand from car group by brand having count(*)>3
二、高級(jí)查詢
1.連接查詢,對(duì)結(jié)果集列的擴(kuò)展
select * from info,nation ? ? ?#形成很大的冗余(笛卡爾積)
多張表的列有重名的,要寫表名,然后寫列名,格式如下:表名.列名
兩種方式:
(1)select * from info,nation where info.nation=nation.code
select info.code,info.name,sex,nation.name,birthday from info,nation where
info.nation=nation.code
(2)select * from info join nation on info.nation=nation.code
2.聯(lián)合查詢,對(duì)結(jié)果集行的擴(kuò)展, 列的數(shù)量要相同 union
select code,name from info
union
select code,name from nation
3.子查詢
父查詢:外層查詢
子查詢:里查詢(查詢結(jié)果作為父查詢的條件)
(1)無關(guān)子查詢:子查詢?cè)趫?zhí)行時(shí)和父查詢沒有關(guān)系(子查詢可單獨(dú)執(zhí)行)
a.查詢民族為漢族的所有人員信息
父查詢:select * from info where nation=()
子查詢:select code from nation where name='漢族'
合并后就是結(jié)果:
select * from info where nation=(select code from nation where name='漢族')
b.查詢系列名是“寶馬5系”的所有汽車信息
select * from car where brand=(select brand_code from brand where brand_name='寶馬5系')
(2)相關(guān)子查詢:子查詢?cè)趫?zhí)行時(shí)和父查詢有關(guān)系(子查詢不可單獨(dú)執(zhí)行)
a.查詢汽車表中油耗小于該系列平均油耗的所有汽車信息
父查詢:select * from car where oil
子查詢:select avg(oil) from car where brand=該系列
合并后就是結(jié)果:
select * from caras a where oil
注意:用as修改表名時(shí)不用加引號(hào)''
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的mysql 语句检查_mysql查询语句的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux cmake编译安装mysql
- 下一篇: matlab 读取mov文件格式,Vid