mysql 查詢條件


  1簡單查詢   select* from 表名

                       select name as“姓名”fromstu     (把name改為名字)

2條件查詢 where 后面跟條件  條件要寫清楚

3模糊查詢  like  no like    %代表任意多個字符   _代表一個字符

4排序查詢  order by 字段 排序值 (desc降 asc升)

5范圍查詢     between....and...

6離散查詢  in  notin

7聚合查詢   sun 求和   count數據條數   max最大值  min最小值 avg平均值

8分頁查詢 limit 從第幾條開始,取多少數據

                 表名limit(pagesize-1)*5,5     pagesize是頁數   *5 是每頁條數 ,5 取多少條

9去重查詢 distinct      select distinct  表段名 from 表名

10分組查詢 group by 字段  having 條件

select count(*),cno,group_concat(degree),sum(degree) from score group by cno ;

select cno,group_concat(degree),sum(degree) from score group by cno having count(*)>3

#分組之后根據條件查詢使用having 不使用where

高級查詢

 

  1. 連接查詢,對列的擴展

   Select * from student as stu,score as sc

where stu.sno = sc.sno and sc.sno = “103” ;

 

2.聯合查詢,對行的擴展

 

    select Code,Name from Info

    union

    select Code,Name from Nation

 

3.子查詢

 

(1)無關子查詢

 

外層查詢 (里層查詢)

子查詢的結果當做父查詢的條件

 

子查詢:select Code from Nation where Name='漢族'

父查詢:select * from Info where Nation = ''

 

    select * from Info where Nation = (select Code from Nation where Name='漢族')

 

 

(2)相關子查詢

    查詢汽車表中油耗低於該系列平均油耗的所有汽車信息

 

    父查詢:select * from Car where Oil<(該系列平均油耗)

    子查詢:select avg(Oil) from Car where Brand = '某個系列'

 

    select * from Car a where Oil<(select avg(Oil) from Car b where b.Brand = a.Brand )


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM