搜尋
首頁資料庫mysql教程MySQL中有哪些資料查詢語句

MySQL中有哪些資料查詢語句

May 26, 2023 pm 02:16 PM
mysql

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

    ①基本語句

    1、「select * from 表名; ”,—可查詢表中全部資料;
    2、“select 欄位名稱from 表名;”,—可查詢表中指定欄位的資料;
    3、“select distinct 欄位名稱from 表名;”,—可對錶中資料進行去重查詢。
    4、“select 欄位名稱from 表名where 查詢條件;”,—可根據條件查詢表中指定欄位的資料;

    ②條件查詢

    #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歲的所有學生的名字

    select * from students where age=18;

    2)邏輯運算子:and,或, not

    –18到28之間的學生資訊

    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歲以上的女性這個範圍內的信息

    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 "^周.*";

    查詢以周開始、倫結尾的姓名

    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不非連續的範圍之內
    年齡不是18、34歲之間的信息

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

    between … and …表示在一個連續的範圍內
    查詢年齡在18到34之間的信息

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

    not between … and …表示不在一個連續的範圍內
    查詢年齡不在在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():查詢內容, having

    where :是對整個資料表資訊的判斷;
    having:是對於分組後的資料進行判斷
    –group by
    依照性別分組,查詢所有的性別

    select gender from students group by gender;

    –計算每個性別中的人數

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

    where是在group by前面
    –計算男性的人數

    select count(*) from students where gender=&#39;男&#39;;

    –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;

    查詢每種性別中的人數多於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:表示数量)
    即limit(第N页-1)*每个的个数,每页的个数;
    limit在使用的时候,要放在最后面.

    限制查詢出來的資料數量

    select *from students where gender=1 limit 2;

    查詢前5個資料

    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)聚合函數:count(), max() , min(), sum(), avg(), round()

    聚合函數
    -總數-- count
    -查詢男性有多少人,女性有多少人

    select count(*) from students where gender=1;
    select count(*) as 男性人数 from students where gender=1;
    select count(*) as 女性人数 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位小数
    –计算所有人的平均年龄,保留2位小数

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

    –计算男性的平均身高保留2位小数

    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;

    查询有能够对应班级的学生以及班级信息

    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;

    给数据表起名字

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

    查询有能够对应班级的学生以及班级信息,显示学生的所有信息,只显示班级名称

    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;

    查询有能够对应班级的学生以及班级信息,按照班级进行排序
    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;

    当时同一个班级的时候,按照学生的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
    查询每位学生对应的班级信息

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

    查询没有对应班级信息的学生
    – 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 别名; 
    消除重复行: distinct 字段

    注意:WHERE子句中是不能用聚集函数作为条件表达式的!

    二、总结

    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

    假设现在又有一个学生成绩表(result)。要求查询一个学生的总成绩。我们根据学号将他们分为了不同的组。

    命令:

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

    现在有两个表学生表(stu)和成绩表(result)。

    5.等值查询

    当连接运算符为“=”时,为等值连接查询。

    现在要查询年龄小于20岁学生的不及格成绩。

    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;

    左外连接就是左表过滤的结果必须全部存在。右表中如果没有与左表过滤出来的数据匹配的记录,那么就会出现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;

    右外连接就是左表过滤的结果必须全部存在

    7.内连接查询

    ①语法

    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关键字是,数据库系统会将所有的查询结果合并到一起,然后去掉相同的记录;

    select id 
    from t_book  
     union 
    select id 
    from t_bookType;

    ②.union all

    使用union all,不会去除掉重复的记录;

    select id 
    from t_book  
     union all 
    select id 
    from t_bookType;

    以上是MySQL中有哪些資料查詢語句的詳細內容。更多資訊請關注PHP中文網其他相關文章!

    陳述
    本文轉載於:亿速云。如有侵權,請聯絡admin@php.cn刪除
    MySQL與Sqlite有何不同?MySQL與Sqlite有何不同?Apr 24, 2025 am 12:12 AM

    MySQL和SQLite的主要區別在於設計理念和使用場景:1.MySQL適用於大型應用和企業級解決方案,支持高性能和高並發;2.SQLite適合移動應用和桌面軟件,輕量級且易於嵌入。

    MySQL中的索引是什麼?它們如何提高性能?MySQL中的索引是什麼?它們如何提高性能?Apr 24, 2025 am 12:09 AM

    MySQL中的索引是數據庫表中一列或多列的有序結構,用於加速數據檢索。 1)索引通過減少掃描數據量提升查詢速度。 2)B-Tree索引利用平衡樹結構,適合範圍查詢和排序。 3)創建索引使用CREATEINDEX語句,如CREATEINDEXidx_customer_idONorders(customer_id)。 4)複合索引可優化多列查詢,如CREATEINDEXidx_customer_orderONorders(customer_id,order_date)。 5)使用EXPLAIN分析查詢計劃,避

    說明如何使用MySQL中的交易來確保數據一致性。說明如何使用MySQL中的交易來確保數據一致性。Apr 24, 2025 am 12:09 AM

    在MySQL中使用事務可以確保數據一致性。 1)通過STARTTRANSACTION開始事務,執行SQL操作後用COMMIT提交或ROLLBACK回滾。 2)使用SAVEPOINT可以設置保存點,允許部分回滾。 3)性能優化建議包括縮短事務時間、避免大規模查詢和合理使用隔離級別。

    在哪些情況下,您可以選擇PostgreSQL而不是MySQL?在哪些情況下,您可以選擇PostgreSQL而不是MySQL?Apr 24, 2025 am 12:07 AM

    選擇PostgreSQL而非MySQL的場景包括:1)需要復雜查詢和高級SQL功能,2)要求嚴格的數據完整性和ACID遵從性,3)需要高級空間功能,4)處理大數據集時需要高性能。 PostgreSQL在這些方面表現出色,適合需要復雜數據處理和高數據完整性的項目。

    如何保護MySQL數據庫?如何保護MySQL數據庫?Apr 24, 2025 am 12:04 AM

    MySQL數據庫的安全可以通過以下措施實現:1.用戶權限管理:通過CREATEUSER和GRANT命令嚴格控制訪問權限。 2.加密傳輸:配置SSL/TLS確保數據傳輸安全。 3.數據庫備份和恢復:使用mysqldump或mysqlpump定期備份數據。 4.高級安全策略:使用防火牆限制訪問,並啟用審計日誌記錄操作。 5.性能優化與最佳實踐:通過索引和查詢優化以及定期維護兼顧安全和性能。

    您可以使用哪些工具來監視MySQL性能?您可以使用哪些工具來監視MySQL性能?Apr 23, 2025 am 12:21 AM

    如何有效監控MySQL性能?使用mysqladmin、SHOWGLOBALSTATUS、PerconaMonitoringandManagement(PMM)和MySQLEnterpriseMonitor等工具。 1.使用mysqladmin查看連接數。 2.用SHOWGLOBALSTATUS查看查詢數。 3.PMM提供詳細性能數據和圖形化界面。 4.MySQLEnterpriseMonitor提供豐富的監控功能和報警機制。

    MySQL與SQL Server有何不同?MySQL與SQL Server有何不同?Apr 23, 2025 am 12:20 AM

    MySQL和SQLServer的区别在于:1)MySQL是开源的,适用于Web和嵌入式系统,2)SQLServer是微软的商业产品,适用于企业级应用。两者在存储引擎、性能优化和应用场景上有显著差异,选择时需考虑项目规模和未来扩展性。

    在哪些情況下,您可以選擇SQL Server而不是MySQL?在哪些情況下,您可以選擇SQL Server而不是MySQL?Apr 23, 2025 am 12:20 AM

    在需要高可用性、高級安全性和良好集成性的企業級應用場景下,應選擇SQLServer而不是MySQL。 1)SQLServer提供企業級功能,如高可用性和高級安全性。 2)它與微軟生態系統如VisualStudio和PowerBI緊密集成。 3)SQLServer在性能優化方面表現出色,支持內存優化表和列存儲索引。

    See all articles

    熱AI工具

    Undresser.AI Undress

    Undresser.AI Undress

    人工智慧驅動的應用程序,用於創建逼真的裸體照片

    AI Clothes Remover

    AI Clothes Remover

    用於從照片中去除衣服的線上人工智慧工具。

    Undress AI Tool

    Undress AI Tool

    免費脫衣圖片

    Clothoff.io

    Clothoff.io

    AI脫衣器

    Video Face Swap

    Video Face Swap

    使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

    熱工具

    mPDF

    mPDF

    mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

    VSCode Windows 64位元 下載

    VSCode Windows 64位元 下載

    微軟推出的免費、功能強大的一款IDE編輯器

    記事本++7.3.1

    記事本++7.3.1

    好用且免費的程式碼編輯器

    PhpStorm Mac 版本

    PhpStorm Mac 版本

    最新(2018.2.1 )專業的PHP整合開發工具

    ZendStudio 13.5.1 Mac

    ZendStudio 13.5.1 Mac

    強大的PHP整合開發環境