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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

mysql select 查询选后5个_mysql 查询select语句汇总

發布時間:2023/12/31 数据库 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 mysql select 查询选后5个_mysql 查询select语句汇总 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

數據準備:

創建表:

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

);

創建數據:

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

-- 一般列為 數據庫.表.列,數據庫和表名可以省略。多表查詢的時候表名不能省略

可以通過 as 關鍵字對表起別名:

例如: select s.id , s.name from students as s;

也可以列起別名:

例如 select id as 編號, name as 姓名 from students;

用as起別名的時候 as可以省略,列名 或 表名 跟別名:

例如: select s.id 編號, s.name 姓名 from students s;

3 消除重復行查詢(去重) distinct :

select distinct 列1,列2 from 表名;

例如: select distinct id , name , gender from students;

-- 當查詢多個列的時候,會把每條數據作為一個整體去去重復。

4 條件 where

select * from 表名 where 條件;

例如 select * from students where id = 1;

where后面支持多種運算符:比較運算符、邏輯運算符、模糊查詢、范圍查詢、空判斷

4.1 比較運算符:等于= 等于> 小于< 大于等于>= 小于等于<= 不等于!=或<>

例如:

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 邏輯運算符: and or not

select * from students where id > 3 and gender = 0;

-- 在students表中 查詢id比3大的 并且 性別是第一個枚舉對象的。

4.3 模糊查詢: like、rlike

% 表示任意多個任意字符

_ 表示一個任意字符

例如:

select * from students where name like "黃%";

-- 查詢姓黃的人

select * from students where name like "黃_";

-- 查詢名字是兩個字 并且姓黃的人

select * from students where name like "%黃" ;

-- 查詢名字中含有黃字的人

select * from students where name rlike "^黃.*";

-- rlike 后面跟正則表達式。

4.4 范圍查詢:

in 表示在一個非連續的范圍內:

select * from students where id in (1,3,8);

-- 查詢id 是 1或者3或者8的所有人

select * from students where id between 3 and 8;

-- 查詢編號3到8的人

select * from students where id between 3 and 8 and gender =1 ;

-- 查詢編號3到8的男生。

空判斷 null:

判斷是不是空 is null

select * from students where height is null;

-- 查詢沒有填寫身高的人。

判斷非空 is not null

select * from students where height is not null;

-- 查詢填身高的人的信息

優先級:

優先級由高到低為: 小括號> not > 比較運算符,邏輯運算符

and 比 or先運算,如果同時出現并希望先算or 就要用小括號。

5 排序:

select * from 表名

order by 列1 asc|desc , 列2 asc|desc

1 先按照列1排序,列1相同的時候按照列2排序。

2 默認按照列值從校到大排序列

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升序進行排序。

6 聚合函數:

1 count(*) 查詢總行數

select count(*) from students;

-- 查詢students表中一共有多少行數據。

2 max(列) 查詢該列的最大值

select max(age) from students;

-- 查詢students中age的最大值

3 min(列) 查詢該列中最小的值

select min(age) from students;

-- 查詢students中age的最小數據

4 sum(列) 查詢該列的數值總和

select sum(age) from students;

-- 查詢students表中age的總和

5 avg(列) 查詢該列的平均值

select avg(age) from students;

-- 查詢students列中age的平均值

7 分組

按照字段分組 表示此字段相同的數據會被放到一個組中

分組后 分組的依據列會顯示在結果集中,其他列不會顯示在結果集中

可以對分組后的數據進行統計,做聚合運算

select 列1 , 列2 ,聚合 ... from 表名 group by 列1,列2 [having 聚合條件] ;

例如:

select gender as 性別 ,count(*)

from students

group by gender;

-- 查詢每個性別的總人數

select age as 年齡, count(*) as 數量

from students

group by age;

-- 查詢每個年齡的人數

分組后的數據篩選:

select 列1,列2,聚合 ... from 表名

group by 列1,列2,列3...

having 列1,...聚合...

-- having后面的條件運算符與where的相同

例如:

select gender as 性別,count(*)

from students

group by gender

having gender = 1;

-- 查詢男生的總人數、

select gender , avg(age) from students group by gender having avg(age)>3;

-- 查詢平均年齡大于3的性別有哪些。

對比where 與 having

where 對from 后面 指定的表進行數據篩選,屬于對原始數據的篩選

having 是對group by 的結果進行篩選

8 分頁 limit :

select * from 表名

limit start , count

-- 略過前start調信息,展示count條信息

-- start可以省略,默認從0開始

事例:

限定每頁顯示m條數據,當前顯示第n頁,求總頁數。

select * from 表名

limit (n-1)*m , m;

9 連接查詢:

mysql支持三種類型的連接查詢:內連接、右外連接、左外連接。

select * from 表名

[inner|across|left|right] join 表2 on 表1.列 = 表2.列(條件)

inner|across 代表內連接

left|right 代表外連接

內連接:嚴格按照條件,兩個表必須都嚴格符合條件。任何一個表不符合條件的都不能進入結果。

select students.* , classes.*

from students

[inner|across] join classes

on students.cls_id = classes.id;

-- 按照班級號相同把兩張表內連接,后展示結果

-- 某個class.id 或者students.cls_id 如果在另一個表中沒有對應的就不會被查出來

左外連接:右側表符合條件的數據和左表全部數據拼接,右表找不到跟左表拼接的數據就用null占位。

select students.* , classes.*

from students

left [outer] join classes

on students.cls_id = classes.id;

-- 以class為主表,所有數據都顯示,如果students中沒有符合條件的 就用null占位

右外連接:左表符合條件的數據和右表全部拼接,左表找不到跟右表拼接的數據用null占位。

select students.* , classes.*

from students

right [outer] join classes

on students.cls_id = classes.id;

-- 以students為主表,所有數據都會顯示。如果classes表中沒有符合的數據,就用null占位。

10 自關聯:

有如下情況:設計省-市-區-縣。。。的數據庫的時候,

省: province: id ptitle 省編號 、 省名稱

市: city: id ctitle proid 市編號 、 市名稱 和 所屬省的編號

區: area: id atitle citid 區編號 、 區名稱 和 所屬市的編號

我們發現,一個省有多個市,一個市有多個區和縣。這樣創建三張表里面結構基本一樣。

而且當進行多表操作的時候,難度實際上是非常大的。

所以這種情況經常用到自聯結。

我們改變表結構:對于省市區只建一張表:

area: aid 、 atitle 、 pid

編號 名稱 上級編號 對于省和直轄市 pid為null

這樣我們想關聯查詢上下級數據的時候,需要用到自關聯。

創建表:

create table area(

aid int primary key, -- 自己的編號

atitle varchar(20), -- 自己的名稱

pid int -- 上級的編號

);

自關聯語句:

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 = "廣州市";

-- 查詢廣州市下轄的所有區

11 子查詢:

常用關鍵字:

in (): where 列 in () 括號中存在就符合條件

any|some (): where 列 = any() 括號中任意一個

all(): where 列 = all() 列匹配里面所有

在一個select語句中嵌套另一個select語句

子查詢分為:

標量子查詢:子查詢返回一個數據(一行一列)

列子查詢:子查詢返回一個列(多行一列)

行子查詢:子查詢返回一個行(一行多列)

表級子查詢:子查詢返回一個表(多行多列)

標量子查詢:

-- 查詢全班大于平均年齡的學生

select *

from students

where students.age > (

select avg(age)

from students

)

列級子查詢:

-- 查詢班級名為python的所有學生信息

select *

from students

where students.cls_id in (

select id

from classes

where classes.name="python"

);

行級子查詢:

-- 查詢年齡和身高同時具備全班最大值的學生

select *

from students

where ( age , height ) in (

select max(age), max(height)

from students

);

表級子查詢:

-- 查詢學生與班級對應信息(表級子查詢一定更要寫別名)

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:任意\某個\全部

-- id 大于 任意一個 就是id大于最小值

select classes.name from classes where id > any(select cls_id from students);

-- 等于任意一個 等于 某一個 意義相同 和 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 結果一樣,相當于不在里面。

select classes.name from classes where id <> all(select cls_id from students);

很多子查詢可以避免,用表連接進行替代。

推薦使用多表 連接,語句清晰,查詢速度也更快。

總結

以上是生活随笔為你收集整理的mysql select 查询选后5个_mysql 查询select语句汇总的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。