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

MySQL中數(shù)據(jù)查詢語句整理大全

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

一、基本概念(查詢語句)

①基本語句

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

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

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

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

②條件查詢

1)比較運算符:>, <, >=, <=, =, !=, <>

查詢大于18歲的信息

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

查詢小于18歲的信息

select * from students where age<18;

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

select * from students where age=18;

2)邏輯運算符: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以上或者身高查過180(包含)以上

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

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

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

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

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

3)模糊查詢:like, rlike

% 替換1個或者多個

_ 替換1個

查詢姓名中 以“小”開始的名字

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

查詢姓名中有“小”所有的名字

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

查詢有2個字的名字

select name from students where name like "__";

查詢有3個字的名字

select name from students where name like "__";

查詢至少有2個字的名字 select name from

students where name like "__%";

rlike正則
查詢以周開始的姓名

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

查詢以周開始、倫結(jié)尾的姓名

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

4)范圍查詢:in,not in,between…and,not between…and

查詢年齡為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 …表示在一個連續(xù)的范圍內(nèi)

查詢年齡在18到34之間的的信息

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

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

查詢年齡不在在18到34之間的的信息

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

空判斷

判空 is null

查詢身高為空的信息

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

判非空is not null

select * from students where height is not null;

排序:order_by

–查詢年齡在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;

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

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

order by多個字段

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

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

查詢年齡在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():查詢內(nèi)容, having

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

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

–group by

按照性別分組,查詢所有的性別

select gender from students group by gender; 

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

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

where是在group by 前面

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

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

–group_concat(…)

查詢同種性別中的姓名

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

having :having是在group by后面

查詢平均年齡超過30歲的性別,以及姓名

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

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

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

– 查詢每組性別的平均年齡

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

分頁: limit

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

限制查詢出來的數(shù)據(jù)個數(shù)

select *from students where gender=1 limit 2;

查詢前5個數(shù)據(jù)

select* from students limit 0,5;

查詢id6-10(包含)的書序

select * from students limit 5,5;

每頁顯示2個,第1個頁面

select * from students limit 0,2;

每頁顯示2個,第2個頁面

select * from students limit 2,2;

每頁顯示2個,第3個頁面

select * from students limit 4,2;

每頁顯示2個,第4個頁面

select * from students limit 6,2;

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

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

– 如果重新排序了,那么會顯示第一頁

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

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

聚合函數(shù)

-總數(shù)-- count

-查詢男性有多少人,女性有多少人

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

一查詢最大的年齡

select max (age) from students;

–查詢女性的最高身高

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

-求和

–sum

-計算所有人的年齡總和

select sum ( age) from students;

–平均值

– avg

–計算平均年齡

select avg(age) from students;

–計算平均年齡

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

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

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

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

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

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

6)連接查詢 :inner join, left join, right join

inner join

select … from 表 A inner join表B;

select * from students inner join classes;

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

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

按照要求顯示姓名、班級

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;

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

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

在以上的查詢中,將班級姓名顯示在第1列

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

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

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;

當時同一個班級的時候,按照學(xué)生的id進行從小到大排序

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

查詢每位學(xué)生對應(yīng)的班級信息

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

查詢沒有對應(yīng)班級信息的學(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是按照左邊的表為基準和右邊的表進行查詢,查到就顯示,查不到就顯示為null

補充

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

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

二、總結(jié)

1、普通查詢

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

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

2、去重查詢(distinct)

命令:select distinct <要查詢的字段> from <表名>

3、排序查詢(order by)

升序:asc

降序:desc

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

不加desc一般默認為升序排列

4、分組查詢(group by)

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

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

命令:

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

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

5.等值查詢

當連接運算符為“=”時,為等值連接查詢。

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

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

等值查詢效率太低

6.外連接查詢

①語法

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

②左外連接查詢,例如

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

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

③右外連接查詢,例如

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

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

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

①語法

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

②例如

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

8.合并查詢

在圖書表(t_book)和圖書類別表(t_bookType)中

①.union

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

select id 
from t_book  
 union 
select id 
from t_bookType;

②.union all

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

select id 
from t_book  
 union all 
select id 
from t_bookType;

總結(jié)

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

相關(guān)文章

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

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

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

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

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

    Ubuntu 18.04配置mysql以及配置遠程連接的步驟

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

    詳解MySQL中的pid與socket

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

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

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

    MYSQL實現(xiàn)連續(xù)簽到功能斷簽一天從頭開始(sql語句)

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

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

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

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

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

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

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

    MySQL跨表查詢與跨表更新

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

最新評論