MySQL查询的方法_MYSQL 查询方法
mysql表格查詢方法:
查詢:
1.簡單查詢
select * from Info --查所有數(shù)據(jù)
select Code,Name from Info --查指定列的數(shù)據(jù)
select Code as ‘代號(hào)‘,Name as ‘姓名‘ from Info --給列指定別名
2.條件查詢
select * from Info where Code=‘p001‘
select * from Info where Sex=‘true‘ and Nation=‘n001‘ --多條件并的關(guān)系
select * from Info where Sex=‘true‘ or Nation=‘n001‘ --多條件或的關(guān)系
3.范圍查詢
select * from Car where Price>40 and Price<50
select * from Car where Price between 40 and 50
4.離散查詢
select * from Car where Code in (‘c001‘,‘c005‘,‘c010‘,‘c015‘)
select * from Car where Code not in (‘c001‘,‘c005‘,‘c010‘,‘c015‘)
5.模糊查詢
select * from Car where Name like ‘%寶馬%‘ --查包含寶馬的
select * from Car where Name like ‘寶馬%‘ --查以寶馬開頭的
select * from Car where Name like ‘%寶馬‘ --查以寶馬結(jié)尾的
select * from Car where Name like ‘寶馬‘ --查等于寶馬的
select * from Car where Name like ‘__E%‘ --查第三個(gè)字符是E的
% 代表是任意多個(gè)字符
_ 代表是一個(gè)字符
6.排序查詢
select * from Car order by Price asc --以價(jià)格升序排列
select * from Car order by Price desc --以價(jià)格降序排列
select * from Car order by Oil desc,Price asc --以兩個(gè)字段排序,前面的是主條件后面的是次要條件
7.分頁查詢
select top 5 * from Car
select top 5 * from Car where Code not in (select top 5 Code from Car)
當(dāng)前頁:page = 2; 每頁顯示:row = 10;
select top row * from Car where Code not in (select top (page-1)*row Code from Car)
8.去重查詢
select distinct Brand from Car
9.分組查詢
select Brand from Car group by Brand having count(*)>2
10.聚合函數(shù)(統(tǒng)計(jì)查詢)
select count(*) from Car --查詢所有數(shù)據(jù)條數(shù)
select count(Code) from Car --查詢所有數(shù)據(jù)條數(shù)
select sum(Price) from Car --求和
select avg(Price) from Car --求平均
select max(Price) from Car --求最大值
select min(Price) from Car --求最小值
高級(jí)查詢
1.連接查詢
select * from Info,Nation --形成笛卡爾積
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
select * from Info join Nation on Info.Nation = Nation.Code --join on 的形式
2.聯(lián)合查詢
select Code,Name from Info
union
select Code,Name from Nation
3.子查詢
一條SQL語句中包含兩個(gè)查詢,其中一個(gè)是父查詢(外層查詢),另一個(gè)是子查詢(里層查詢),子查詢查詢的結(jié)果作為父查詢的條件。
--查詢民族為漢族的所有人員信息
select * from Info where Nation = (select Code from Nation where Name = ‘漢族‘)
(1)無關(guān)子查詢
子查詢可以單獨(dú)執(zhí)行,子查詢和父查詢沒有一定的關(guān)系
--查詢系列是寶馬5系的所有汽車信息
select * from Car where Brand =(select Brand_Code from Brand where Brand_Name = ‘寶馬5系‘)
(2)相關(guān)子查詢
--查找油耗低于該系列平均油耗的汽車
select * from Car where Oil
select avg(Oil) from Car where Brand = (該系列)
select * from Car a where Oil
MYSQL 查詢方法
標(biāo)簽:數(shù)據(jù)???ati???esc???相關(guān)子查詢???code???相關(guān)???笛卡爾積???卡爾???等于
本條技術(shù)文章來源于互聯(lián)網(wǎng),如果無意侵犯您的權(quán)益請(qǐng)點(diǎn)擊此處反饋版權(quán)投訴
本文系統(tǒng)來源:http://www.cnblogs.com/dej-11/p/7580467.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的MySQL查询的方法_MYSQL 查询方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql mybatis cdata_
- 下一篇: spy导入数据到oracle,运用Sch