mysql select 查询选后5个_mysql 查询select语句汇总
數(shù)據(jù)準(zhǔn)備:
創(chuàng)建表:
create table students(
id int unsigned primary key auto_increment not null,
name varchar(20) default '',
age tinyint unsigned default 0,
height decimal(5,2),
gender enum('男','女','人妖','保密'),
cls_id int unsigned default 0,
isdelete bit default 0
);
創(chuàng)建數(shù)據(jù):
insert into students values
(0,'韋少',18,180.00,2,1,0),
(0,'小月月',18,180.00,2,2,1),
(0,'彭于晏',29,185.00,1,1,0),
(0,'劉德華',59,175.00,1,2,1),
(0,'芙蓉',38,160.00,2,1,0),
(0,'鳳姐',28,150.00,4,2,1),
(0,'王祖賢',18,172.00,2,1,1),
(0,'周杰倫',36,NULL,1,1,0),
(0,'程坤',27,181.00,1,2,0),
(0,'劉亦菲',25,166.00,2,2,0),
(0,'金星',33,162.00,3,3,1),
(0,'靜香',12,180.00,2,4,0),
(0,'周杰',34,176.00,2,5,0);
1 查詢所有字段:
select * from 表名;
例如 select * from students;
-- 查詢students表中的所有信息
2 指定字段查詢:
select 列1 , 列2 , ... , 列n from 表名;
例如 select students.id, students.name from students
-- 一般列為 數(shù)據(jù)庫.表.列,數(shù)據(jù)庫和表名可以省略。多表查詢的時(shí)候表名不能省略
可以通過 as 關(guān)鍵字對(duì)表起別名:
例如: select s.id , s.name from students as s;
也可以列起別名:
例如 select id as 編號(hào), name as 姓名 from students;
用as起別名的時(shí)候 as可以省略,列名 或 表名 跟別名:
例如: select s.id 編號(hào), s.name 姓名 from students s;
3 消除重復(fù)行查詢(去重) distinct :
select distinct 列1,列2 from 表名;
例如: select distinct id , name , gender from students;
-- 當(dāng)查詢多個(gè)列的時(shí)候,會(huì)把每條數(shù)據(jù)作為一個(gè)整體去去重復(fù)。
4 條件 where
select * from 表名 where 條件;
例如 select * from students where id = 1;
where后面支持多種運(yùn)算符:比較運(yùn)算符、邏輯運(yùn)算符、模糊查詢、范圍查詢、空判斷
4.1 比較運(yùn)算符:等于= 等于> 小于< 大于等于>= 小于等于<= 不等于!=或<>
例如:
select * from students where id = 1;
-- 在students表中搜索id是1的。
select * from students where id >5 ;
-- 在students表中查詢id比5大的。
select * from students where name != "黃蓉";
-- 查詢名字 不是黃蓉的信息。
4.2 邏輯運(yùn)算符: and or not
select * from students where id > 3 and gender = 0;
-- 在students表中 查詢id比3大的 并且 性別是第一個(gè)枚舉對(duì)象的。
4.3 模糊查詢: like、rlike
% 表示任意多個(gè)任意字符
_ 表示一個(gè)任意字符
例如:
select * from students where name like "黃%";
-- 查詢姓黃的人
select * from students where name like "黃_";
-- 查詢名字是兩個(gè)字 并且姓黃的人
select * from students where name like "%黃" ;
-- 查詢名字中含有黃字的人
select * from students where name rlike "^黃.*";
-- rlike 后面跟正則表達(dá)式。
4.4 范圍查詢:
in 表示在一個(gè)非連續(xù)的范圍內(nèi):
select * from students where id in (1,3,8);
-- 查詢id 是 1或者3或者8的所有人
select * from students where id between 3 and 8;
-- 查詢編號(hào)3到8的人
select * from students where id between 3 and 8 and gender =1 ;
-- 查詢編號(hào)3到8的男生。
空判斷 null:
判斷是不是空 is null
select * from students where height is null;
-- 查詢沒有填寫身高的人。
判斷非空 is not null
select * from students where height is not null;
-- 查詢填身高的人的信息
優(yōu)先級(jí):
優(yōu)先級(jí)由高到低為: 小括號(hào)> not > 比較運(yùn)算符,邏輯運(yùn)算符
and 比 or先運(yùn)算,如果同時(shí)出現(xiàn)并希望先算or 就要用小括號(hào)。
5 排序:
select * from 表名
order by 列1 asc|desc , 列2 asc|desc
1 先按照列1排序,列1相同的時(shí)候按照列2排序。
2 默認(rèn)按照列值從校到大排序列
3 asc從小到大排列,升序
4 desc從大到小排序,降序
例如:
select * from students
where gender = 1
order by id desc;
-- 在students表中查詢gender是1的 并且按照id的降序排列
select * from students
where isdelete = 0
order by name;
-- 在students表中沒有刪除的信息 按照name升序進(jìn)行排序。
6 聚合函數(shù):
1 count(*) 查詢總行數(shù)
select count(*) from students;
-- 查詢students表中一共有多少行數(shù)據(jù)。
2 max(列) 查詢?cè)摿械淖畲笾?/p>
select max(age) from students;
-- 查詢students中age的最大值
3 min(列) 查詢?cè)摿兄凶钚〉闹?/p>
select min(age) from students;
-- 查詢students中age的最小數(shù)據(jù)
4 sum(列) 查詢?cè)摿械臄?shù)值總和
select sum(age) from students;
-- 查詢students表中age的總和
5 avg(列) 查詢?cè)摿械钠骄?/p>
select avg(age) from students;
-- 查詢students列中age的平均值
7 分組
按照字段分組 表示此字段相同的數(shù)據(jù)會(huì)被放到一個(gè)組中
分組后 分組的依據(jù)列會(huì)顯示在結(jié)果集中,其他列不會(huì)顯示在結(jié)果集中
可以對(duì)分組后的數(shù)據(jù)進(jìn)行統(tǒng)計(jì),做聚合運(yùn)算
select 列1 , 列2 ,聚合 ... from 表名 group by 列1,列2 [having 聚合條件] ;
例如:
select gender as 性別 ,count(*)
from students
group by gender;
-- 查詢每個(gè)性別的總?cè)藬?shù)
select age as 年齡, count(*) as 數(shù)量
from students
group by age;
-- 查詢每個(gè)年齡的人數(shù)
分組后的數(shù)據(jù)篩選:
select 列1,列2,聚合 ... from 表名
group by 列1,列2,列3...
having 列1,...聚合...
-- having后面的條件運(yùn)算符與where的相同
例如:
select gender as 性別,count(*)
from students
group by gender
having gender = 1;
-- 查詢男生的總?cè)藬?shù)、
select gender , avg(age) from students group by gender having avg(age)>3;
-- 查詢平均年齡大于3的性別有哪些。
對(duì)比where 與 having
where 對(duì)from 后面 指定的表進(jìn)行數(shù)據(jù)篩選,屬于對(duì)原始數(shù)據(jù)的篩選
having 是對(duì)group by 的結(jié)果進(jìn)行篩選
8 分頁 limit :
select * from 表名
limit start , count
-- 略過前start調(diào)信息,展示count條信息
-- start可以省略,默認(rèn)從0開始
事例:
限定每頁顯示m條數(shù)據(jù),當(dāng)前顯示第n頁,求總頁數(shù)。
select * from 表名
limit (n-1)*m , m;
9 連接查詢:
mysql支持三種類型的連接查詢:內(nèi)連接、右外連接、左外連接。
select * from 表名
[inner|across|left|right] join 表2 on 表1.列 = 表2.列(條件)
inner|across 代表內(nèi)連接
left|right 代表外連接
內(nèi)連接:嚴(yán)格按照條件,兩個(gè)表必須都嚴(yán)格符合條件。任何一個(gè)表不符合條件的都不能進(jìn)入結(jié)果。
select students.* , classes.*
from students
[inner|across] join classes
on students.cls_id = classes.id;
-- 按照班級(jí)號(hào)相同把兩張表內(nèi)連接,后展示結(jié)果
-- 某個(gè)class.id 或者students.cls_id 如果在另一個(gè)表中沒有對(duì)應(yīng)的就不會(huì)被查出來
左外連接:右側(cè)表符合條件的數(shù)據(jù)和左表全部數(shù)據(jù)拼接,右表找不到跟左表拼接的數(shù)據(jù)就用null占位。
select students.* , classes.*
from students
left [outer] join classes
on students.cls_id = classes.id;
-- 以class為主表,所有數(shù)據(jù)都顯示,如果students中沒有符合條件的 就用null占位
右外連接:左表符合條件的數(shù)據(jù)和右表全部拼接,左表找不到跟右表拼接的數(shù)據(jù)用null占位。
select students.* , classes.*
from students
right [outer] join classes
on students.cls_id = classes.id;
-- 以students為主表,所有數(shù)據(jù)都會(huì)顯示。如果classes表中沒有符合的數(shù)據(jù),就用null占位。
10 自關(guān)聯(lián):
有如下情況:設(shè)計(jì)省-市-區(qū)-縣。。。的數(shù)據(jù)庫的時(shí)候,
省: province: id ptitle 省編號(hào) 、 省名稱
市: city: id ctitle proid 市編號(hào) 、 市名稱 和 所屬省的編號(hào)
區(qū): area: id atitle citid 區(qū)編號(hào) 、 區(qū)名稱 和 所屬市的編號(hào)
我們發(fā)現(xiàn),一個(gè)省有多個(gè)市,一個(gè)市有多個(gè)區(qū)和縣。這樣創(chuàng)建三張表里面結(jié)構(gòu)基本一樣。
而且當(dāng)進(jìn)行多表操作的時(shí)候,難度實(shí)際上是非常大的。
所以這種情況經(jīng)常用到自聯(lián)結(jié)。
我們改變表結(jié)構(gòu):對(duì)于省市區(qū)只建一張表:
area: aid 、 atitle 、 pid
編號(hào) 名稱 上級(jí)編號(hào) 對(duì)于省和直轄市 pid為null
這樣我們想關(guān)聯(lián)查詢上下級(jí)數(shù)據(jù)的時(shí)候,需要用到自關(guān)聯(lián)。
創(chuàng)建表:
create table area(
aid int primary key, -- 自己的編號(hào)
atitle varchar(20), -- 自己的名稱
pid int -- 上級(jí)的編號(hào)
);
自關(guān)聯(lián)語句:
select city.*
from area as city
inner join area as province
on city.pid = province.id;
where province.atitle = "山西省"
-- 查詢山西省管轄的所有城市
select dis.*
from area as city
inner join area as dis
on dis.pid = city.id
where city.atitle = "廣州市";
-- 查詢廣州市下轄的所有區(qū)
11 子查詢:
常用關(guān)鍵字:
in (): where 列 in () 括號(hào)中存在就符合條件
any|some (): where 列 = any() 括號(hào)中任意一個(gè)
all(): where 列 = all() 列匹配里面所有
在一個(gè)select語句中嵌套另一個(gè)select語句
子查詢分為:
標(biāo)量子查詢:子查詢返回一個(gè)數(shù)據(jù)(一行一列)
列子查詢:子查詢返回一個(gè)列(多行一列)
行子查詢:子查詢返回一個(gè)行(一行多列)
表級(jí)子查詢:子查詢返回一個(gè)表(多行多列)
標(biāo)量子查詢:
-- 查詢?nèi)啻笥谄骄挲g的學(xué)生
select *
from students
where students.age > (
select avg(age)
from students
)
列級(jí)子查詢:
-- 查詢班級(jí)名為python的所有學(xué)生信息
select *
from students
where students.cls_id in (
select id
from classes
where classes.name="python"
);
行級(jí)子查詢:
-- 查詢年齡和身高同時(shí)具備全班最大值的學(xué)生
select *
from students
where ( age , height ) in (
select max(age), max(height)
from students
);
表級(jí)子查詢:
-- 查詢學(xué)生與班級(jí)對(duì)應(yīng)信息(表級(jí)子查詢一定更要寫別名)
select t.sname , t.cname from (
select s.name as sname , c.name as cname
from students as s inner join classes as c
on s.cls_id = c.id
)as t ;
any\some\all:任意\某個(gè)\全部
-- id 大于 任意一個(gè) 就是id大于最小值
select classes.name from classes where id > any(select cls_id from students);
-- 等于任意一個(gè) 等于 某一個(gè) 意義相同 和 in 類似
select classes.name from classes where id = any(select cls_id from students);
select classes.name from classes where id = some(select cls_id from students);
-- <> all 和 not in 結(jié)果一樣,相當(dāng)于不在里面。
select classes.name from classes where id <> all(select cls_id from students);
很多子查詢可以避免,用表連接進(jìn)行替代。
推薦使用多表 連接,語句清晰,查詢速度也更快。
總結(jié)
以上是生活随笔為你收集整理的mysql select 查询选后5个_mysql 查询select语句汇总的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 程序性知识分为哪三类
- 下一篇: mysql常用命令orderby_MyS