欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MySQL中數(shù)據(jù)查詢(xún)語(yǔ)句整理大全

 更新時(shí)間:2023年04月18日 14:48:16   作者:自xing且樂(lè)觀~  
查詢(xún)語(yǔ)句是以后在工作中使用最多也是最復(fù)雜的用法,如何精準(zhǔn)的查詢(xún)出想要的結(jié)果以及用最合理的邏輯去查詢(xún)尤為重要,下面這篇文章主要給大家介紹了關(guān)于MySQL中數(shù)據(jù)查詢(xún)語(yǔ)句的相關(guān)資料,需要的朋友可以參考下

一、基本概念(查詢(xún)語(yǔ)句)

①基本語(yǔ)句

1、“select * from 表名;”,—可查詢(xún)表中全部數(shù)據(jù);

2、“select 字段名 from 表名;”,—可查詢(xún)表中指定字段的數(shù)據(jù);

3、“select distinct 字段名 from 表名;”,—可對(duì)表中數(shù)據(jù)進(jìn)行去重查詢(xún)。

4、“select 字段名 from 表名 where 查詢(xún)條件;”,—可根據(jù)條件查詢(xún)表中指定字段的數(shù)據(jù);

②條件查詢(xún)

1)比較運(yùn)算符:>, <, >=, <=, =, !=, <>

查詢(xún)大于18歲的信息

select * from students where age>18;
select id, name,gender from students where age>18;

查詢(xún)小于18歲的信息

select * from students where age<18;

查詢(xún)年齡為18歲的所有學(xué)生的名字

select * from students where age=18;

2)邏輯運(yùn)算符:and, or, not

–18到28之間的學(xué)生信息

select * from students where age>18_and age<28:

–18歲以上的女性

select * from students where age>18 and gender="女";
select * from students where age>18 and gender=2;

–18以上或者身高查過(guò)180(包含)以上

select * from students where age>18 or height>=180;

不在18歲以上的女性這個(gè)范圍內(nèi)的信息

select * from students where not (age>18 and gender=2);

年齡不是小于或者等于18并且是女性

select * from students where (not age<=18) and gender=2;

3)模糊查詢(xún):like, rlike

% 替換1個(gè)或者多個(gè)

_ 替換1個(gè)

查詢(xún)姓名中 以“小”開(kāi)始的名字

select name from students where name="小";
select name from students where name like"小%";

查詢(xún)姓名中有“小”所有的名字

select name from students whece name like "%小%";

查詢(xún)有2個(gè)字的名字

select name from students where name like "__";

查詢(xún)有3個(gè)字的名字

select name from students where name like "__";

查詢(xún)至少有2個(gè)字的名字 select name from

students where name like "__%";

rlike正則
查詢(xún)以周開(kāi)始的姓名

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

查詢(xún)以周開(kāi)始、倫結(jié)尾的姓名

select name from students where name rlike "^周.*倫$";

4)范圍查詢(xún):in,not in,between…and,not between…and

查詢(xún)年齡為18、34的姓名

select name, age from students where age=18 or age=34;
select name,age from students where age in (18,34);

not in不非連續(xù)的范圍之內(nèi)

年齡不是 18、34歲之間的信息

select name,age from students where age not in (18,34);

between … and …表示在一個(gè)連續(xù)的范圍內(nèi)

查詢(xún)年齡在18到34之間的的信息

select name,age from students where age between 18 and 34;

not between … and …表示不在一個(gè)連續(xù)的范圍內(nèi)

查詢(xún)年齡不在在18到34之間的的信息

select * from students where age not between 18 and 34;

空判斷

判空 is null

查詢(xún)身高為空的信息

select *from students where height is null/NULL/Null;

判非空is not null

select * from students where height is not null;

排序:order_by

–查詢(xún)年齡在18到34歲之間的男性,按照年齡從小到大排序

select * from students where (age between 18 and 34) and gender=1; 
select * from students where (age between 18 and 34) and gender=1 order by age; 
select * from students where (age between 18 and 34) and gender=1 order by age asc;

查詢(xún)年齡在18到34歲之間的女性,身高從高到矮排序

select * from students where (age between18 and 34) and gender=2 order by height desc;

order by多個(gè)字段

查詢(xún)年齡在18到34歲之間的女性,身高從高到矮排序,如果身高相同的情況下按照年齡從小到大排序

select * from students where (age between 18 and 34) and gender=2 order by height desc,age asc;

查詢(xún)年齡在18到34歲之間的女性,身高從高到矮排序,如果身高相同的情況下按照年齡從小到大排序,如果年齡也相同那么按照id從大到小排序

select * from students where (age between 18 and 34) and gender=2 order by height desc,age asc, id desc;

按照年齡從小到大、身高從高到矮的排序

select * from students order by age asc,height desc;

分組:group_by, group_concat():查詢(xún)內(nèi)容, having

where :是對(duì)整個(gè)數(shù)據(jù)表信息的判斷;

having:是對(duì)于分組后的數(shù)據(jù)進(jìn)行判斷

–group by

按照性別分組,查詢(xún)所有的性別

select gender from students group by gender; 

–計(jì)算每種性別中的人數(shù)

select gender, count(*) from students group by gender; 

where是在group by 前面

–計(jì)算男性的人數(shù)

select count(*) from students where gender='男';

–group_concat(…)

查詢(xún)同種性別中的姓名

select gender,group_concat(name) from students group by gender;

having :having是在group by后面

查詢(xún)平均年齡超過(guò)30歲的性別,以及姓名

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

查詢(xún)每種性別中的人數(shù)多于2個(gè)的信息

select gender,count(*) from students group by gender having count(*) > 2;

– 查詢(xún)每組性別的平均年齡

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

分頁(yè): limit

limit start,count   (start:表示從哪─個(gè)開(kāi)始;count:表示數(shù)量)
即limit(第N頁(yè)-1)*每個(gè)的個(gè)數(shù),每頁(yè)的個(gè)數(shù);
limit在使用的時(shí)候,要放在最后面.

限制查詢(xún)出來(lái)的數(shù)據(jù)個(gè)數(shù)

select *from students where gender=1 limit 2;

查詢(xún)前5個(gè)數(shù)據(jù)

select* from students limit 0,5;

查詢(xún)id6-10(包含)的書(shū)序

select * from students limit 5,5;

每頁(yè)顯示2個(gè),第1個(gè)頁(yè)面

select * from students limit 0,2;

每頁(yè)顯示2個(gè),第2個(gè)頁(yè)面

select * from students limit 2,2;

每頁(yè)顯示2個(gè),第3個(gè)頁(yè)面

select * from students limit 4,2;

每頁(yè)顯示2個(gè),第4個(gè)頁(yè)面

select * from students limit 6,2;

每頁(yè)顯示2個(gè),顯示第6頁(yè)的信息,按照年齡從小到大排序

select * from students order by age asc limit 10,2; 

– 如果重新排序了,那么會(huì)顯示第一頁(yè)

select * from students where gender=2 order by height des limit 0,2;

5)聚合函數(shù):count(), max(), min(), sum(), avg(), round()

聚合函數(shù)

-總數(shù)-- count

-查詢(xún)男性有多少人,女性有多少人

select count(*) from students where gender=1;
select count(*) as 男性人數(shù) from students where gender=1;
select count(*) as 女性人數(shù) from students where gender=2;

-最大值-最小值

– max --min

一查詢(xún)最大的年齡

select max (age) from students;

–查詢(xún)女性的最高身高

select max (height) from students where gender=2;

-求和

–sum

-計(jì)算所有人的年齡總和

select sum ( age) from students;

–平均值

– avg

–計(jì)算平均年齡

select avg(age) from students;

–計(jì)算平均年齡

select sum ( age) / count(* ) from students;

–四舍五入round ( 123.23 ,_1)保留1位小數(shù)

–計(jì)算所有人的平均年齡,保留2位小數(shù)

select round (sum(age)/count(*),2) from students; select round ( sum(age)/count(*),3) from students ;

–計(jì)算男性的平均身高保留2位小數(shù)

select round(avg (height),2) from students where gender=1; select name,round(avg(height),2) from students where gender=1;

6)連接查詢(xún) :inner join, left join, right join

inner join

select … from 表 A inner join表B;

select * from students inner join classes;

查詢(xún)有能夠?qū)?yīng)班級(jí)的學(xué)生以及班級(jí)信息

select * from students inner join classes on students.cls_id=classes.id; 

按照要求顯示姓名、班級(jí)

select students.*, classes.name from students inner join classes on students.cls_id=classes.id;
select students.name,classes.name from students inner join classes on students.cls_id=classes.id; 

給數(shù)據(jù)表起名字

select s.name,c.name from students as s inner join classes as c on s.cls_id=c.id;

查詢(xún)有能夠?qū)?yīng)班級(jí)的學(xué)生以及班級(jí)信息,顯示學(xué)生的所有信息,只顯示班級(jí)名稱(chēng)

select s.*,c.name from students as s inner join classes as c on s.cls_id=c.id;

在以上的查詢(xún)中,將班級(jí)姓名顯示在第1列

select c.name,s.* from students as s inner join classes as c on s.cls_id=c.id;

查詢(xún)有能夠?qū)?yīng)班級(jí)的學(xué)生以及班級(jí)信息,按照班級(jí)進(jìn)行排序

select c.xxx s.xxx from student as s inner join clssses as c on … order by …;

select c.name,s.* from students as s inner join classes as c on s.cls_id=c.id order by c.name;

當(dāng)時(shí)同一個(gè)班級(jí)的時(shí)候,按照學(xué)生的id進(jìn)行從小到大排序

select c.name,s.* from students as s inner join classes as c on s.cls_id=c.id order by c.name,s.id;

left join

查詢(xún)每位學(xué)生對(duì)應(yīng)的班級(jí)信息

select * from students as s left join classes as c on s.cls_id=c.id;

查詢(xún)沒(méi)有對(duì)應(yīng)班級(jí)信息的學(xué)生

– select … from xxx as s left join xxx as c on… where …
– select … from xxx as s left join xxx as c on… . … having …

select * from students as s left join classes as c on s.cls_id=c.id having c.id is null;
select * from students as s left join classes as c on s.cls_id=c.id where c.id is null;

left join是按照左邊的表為基準(zhǔn)和右邊的表進(jìn)行查詢(xún),查到就顯示,查不到就顯示為null

補(bǔ)充

查詢(xún)所有字段:select * from 表名; 
查詢(xún)指定字段:select 列1,列2,... from 表名; 
使用 as 給字段起別名: select 字段 as 名字.... from 表名; 
查詢(xún)某個(gè)表的某個(gè)字段:select 表名.字段 .... from表名; 
可以通過(guò) as 給表起別名: select 別名.字段 .... from 表名 as 別名; 
消除重復(fù)行: distinct 字段

注意:WHERE子句中是不能用聚集函數(shù)作為條件表達(dá)式的!

二、總結(jié)

1、普通查詢(xún)

(1)命令:select * from <表名>;

(2)命令:select <要查詢(xún)的字段> from <表名>;

2、去重查詢(xún)(distinct)

命令:select distinct <要查詢(xún)的字段> from <表名>

3、排序查詢(xún)(order by)

升序:asc

降序:desc

降序排列命令:select <要查詢(xún)的字段名> from <表名> order by <要查詢(xún)的字段名> desc

不加desc一般默認(rèn)為升序排列

4、分組查詢(xún)(group by)

命令:select <按什么分的組>, Sum(score) from <表名> group by <按什么分的組>

假設(shè)現(xiàn)在又有一個(gè)學(xué)生成績(jī)表(result)。要求查詢(xún)一個(gè)學(xué)生的總成績(jī)。我們根據(jù)學(xué)號(hào)將他們分為了不同的組。

命令:

select id, Sum(score) 
from result 
group by id;

現(xiàn)在有兩個(gè)表學(xué)生表(stu)和成績(jī)表(result)。

5.等值查詢(xún)

當(dāng)連接運(yùn)算符為“=”時(shí),為等值連接查詢(xún)。

現(xiàn)在要查詢(xún)年齡小于20歲學(xué)生的不及格成績(jī)。

select stu.id,score 
from stu,result 
where stu.id = result.id and age < 20 and score < 60;

等值查詢(xún)效率太低

6.外連接查詢(xún)

①語(yǔ)法

select f1,f2,f3,....
from table1 left/right outer join table2
on 條件;

②左外連接查詢(xún),例如

select a.id,score
from
  (select id,age from stu where age < 20) a (過(guò)濾左表信息)
left join
  (select id, score from result where score < 60) b (過(guò)濾右表信息)
on a.id = b.id;

左外連接就是左表過(guò)濾的結(jié)果必須全部存在。如果存在左表中過(guò)濾出來(lái)的數(shù)據(jù),右表沒(méi)有匹配上,這樣的話(huà)右表就會(huì)出現(xiàn)NULL;

③右外連接查詢(xún),例如

select a.id,score
from
  (select id,age from stu where age < 20) a (過(guò)濾左表信息)
right join
  (select id, score from result where score < 60) b (過(guò)濾右表信息)
on a.id = b.id;

右外連接就是左表過(guò)濾的結(jié)果必須全部存在

7.內(nèi)連接查詢(xún)

①語(yǔ)法

select f1,f2,f3,....
from table1 inter join table2
on 條件;

②例如

select a.id,score
from
  (select id,age from stu where age < 20) a (過(guò)濾左表信息)
inner join
  (select id, score from result where score < 60) b (過(guò)濾右表信息)
on a.id = b.id;

8.合并查詢(xún)

在圖書(shū)表(t_book)和圖書(shū)類(lèi)別表(t_bookType)中

①.union

使用union關(guān)鍵字是,數(shù)據(jù)庫(kù)系統(tǒng)會(huì)將所有的查詢(xún)結(jié)果合并到一起,然后去掉相同的記錄;

select id 
from t_book  
 union 
select id 
from t_bookType;

②.union all

使用union all,不會(huì)去除掉重復(fù)的記錄;

select id 
from t_book  
 union all 
select id 
from t_bookType;

總結(jié)

到此這篇關(guān)于MySQL中數(shù)據(jù)查詢(xún)語(yǔ)句的文章就介紹到這了,更多相關(guān)MySQL數(shù)據(jù)查詢(xún)語(yǔ)句內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • mysql實(shí)現(xiàn)將date字段默認(rèn)值設(shè)置為CURRENT_DATE

    mysql實(shí)現(xiàn)將date字段默認(rèn)值設(shè)置為CURRENT_DATE

    這篇文章主要介紹了mysql實(shí)現(xiàn)將date字段默認(rèn)值設(shè)置為CURRENT_DATE問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • MyEclipse連接Mysql數(shù)據(jù)庫(kù)的方法(一)

    MyEclipse連接Mysql數(shù)據(jù)庫(kù)的方法(一)

    這篇文章主要介紹了MyEclipse連接Mysql數(shù)據(jù)庫(kù)的方法(一)的相關(guān)資料,非常實(shí)用,具有參考價(jià)值,需要的朋友可以參考下
    2016-05-05
  • Ubuntu 18.04配置mysql以及配置遠(yuǎn)程連接的步驟

    Ubuntu 18.04配置mysql以及配置遠(yuǎn)程連接的步驟

    這篇文章主要給大家介紹了關(guān)于Ubuntu 18.04配置mysql以及配置遠(yuǎn)程連接的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-07-07
  • 詳解MySQL中的pid與socket

    詳解MySQL中的pid與socket

    不知道你有沒(méi)有注意過(guò),MySQL 啟動(dòng)時(shí)需要配置 pid 及 socket 文件路徑。偶爾還會(huì)出現(xiàn)因 pid 文件找不到而啟動(dòng)失敗的現(xiàn)象,那么 pid 與 socket 文件究竟是干什么用的呢?我們一起來(lái)看下本篇文章。
    2021-06-06
  • MySQL過(guò)濾重復(fù)數(shù)據(jù)的兩種方法示例

    MySQL過(guò)濾重復(fù)數(shù)據(jù)的兩種方法示例

    數(shù)據(jù)庫(kù)生成環(huán)境中經(jīng)常會(huì)遇到表中有重復(fù)的數(shù)據(jù),或者進(jìn)行關(guān)聯(lián)過(guò)程中產(chǎn)生重復(fù)數(shù)據(jù),下面這篇文章主要給大家介紹了關(guān)于MySQL過(guò)濾重復(fù)數(shù)據(jù)的兩種方法,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-04-04
  • MYSQL實(shí)現(xiàn)連續(xù)簽到功能斷簽一天從頭開(kāi)始(sql語(yǔ)句)

    MYSQL實(shí)現(xiàn)連續(xù)簽到功能斷簽一天從頭開(kāi)始(sql語(yǔ)句)

    這篇文章主要介紹了MYSQL實(shí)現(xiàn)連續(xù)簽到功能斷簽一天從頭開(kāi)始,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-05-05
  • mysql制作外鍵出現(xiàn)duplicate?key?name錯(cuò)誤問(wèn)題及解決

    mysql制作外鍵出現(xiàn)duplicate?key?name錯(cuò)誤問(wèn)題及解決

    這篇文章主要介紹了mysql制作外鍵出現(xiàn)duplicate?key?name錯(cuò)誤問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • MySQL在不知道列名情況下的注入詳解

    MySQL在不知道列名情況下的注入詳解

    這篇文章主要給大家介紹了關(guān)于MySQL在不知道列名情況下的注入的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用mysql具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • Ubuntu下完美實(shí)現(xiàn)遷移MySQL數(shù)據(jù)庫(kù)位置

    Ubuntu下完美實(shí)現(xiàn)遷移MySQL數(shù)據(jù)庫(kù)位置

    這篇文章主要介紹了Ubuntu下完美實(shí)現(xiàn)遷移MySQL數(shù)據(jù)庫(kù)位置,十分詳細(xì),有需要的小伙伴可以參考下
    2015-03-03
  • MySQL跨表查詢(xún)與跨表更新

    MySQL跨表查詢(xún)與跨表更新

    本文主要給大家講解的是MySQL中如何使用跨表更新的方法和示例,非常的實(shí)用,有需要的小伙伴可以查看查看
    2020-02-02

最新評(píng)論