數(shù)據(jù)庫多表查詢 聯(lián)合查詢 增刪改查

1|*1*****方式一

語法:

    insert into 表名 (字段名,...) values (值,...);

特點(diǎn):

1、要求值的類型和字段的類型要一致或兼容

2、字段的個(gè)數(shù)和順序不一定與原始表中的字段個(gè)數(shù)和順序一致

但必須保證值和字段一一對(duì)應(yīng)

3、假如表中有可以為null的字段,注意可以通過以下兩種方式插入null值

①字段和值都省略

②字段寫上,值使用null

4、字段和值的個(gè)數(shù)必須一致

5、字段名可以省略,默認(rèn)所有列

1|*2*****方式二

語法:

    insert into 表名 set 字段=值,字段=值,...;

1|*3*****兩種方式的區(qū)別:

1.方式一支持一次插入多行,語法如下:

    insert into 表名(字段名,..) values(值,..),(值,...),...;

2.方式二支持子查詢,語法如下:

    insert into 表名

    查詢語句;

2|*0*****修改

一、修改單表的記錄

語法:

    update 表名 set 字段=值,字段=值 where 篩選條件;`

二、修改多表的記錄【補(bǔ)充】

語法:

    update 表1 別名 

    left|right|inner join 表2 別名 

    on 連接條件  

    set 字段=值,字段=值 

    where 篩選條件;

3|*0*****刪除

3|*1*****方式一:使用delete

一、刪除單表的記錄

語法:

delete from 表名 where 篩選條件 limit 條目數(shù)

二、級(jí)聯(lián)刪除[補(bǔ)充]

語法:

    delete 別名1,別名2 from 表1 別名 

    inner|left|right join 表2 別名 

    on 連接條件

    where 篩選條件

3|*2*****方式二:使用truncate

語法:

truncate table 表名

3|*3*****兩種方式的區(qū)別【面試題】

  1. truncate刪除后,如果再插入,標(biāo)識(shí)列從1開始;delete刪除后,如果再插入,標(biāo)識(shí)列從斷點(diǎn)開始
  2. delete可以添加篩選條件;truncate不可以添加篩選條件
  3. truncate效率較高
  4. truncate沒有返回值;delete可以返回受影響的行數(shù)
  5. truncate不可以回滾;delete可以回滾

4|*0*****基礎(chǔ)查詢

**************1.去除重復(fù)的記錄distinct

select distinct address from stu;

**************2.查詢時(shí) as 來別名列明

select name as 名字 ,age as 年齡,from user;

SELECT userCode AS 名字 FROM smbms_user;

4|*1*****語法:

    select 列名1, ... ,列名n from 表名

    where 條件        -- 1、條件

    group by 列名     -- 2、分組

    having 條件       -- 3、條件

    order by 列名     -- 4、排序

    limit 開始,條數(shù)   -- 5、分頁

4|*2*****關(guān)于列的操作

**************1、查詢所有列所有行

    select * from 表名

**************2、查詢指定列

    #多列列名使用,隔開 

    select 列名 from 表名

**************3、給列起別名

    select 列名 [as] 別名,列名n [as] 別名n

**************4、列查詢并進(jìn)行算術(shù)運(yùn)算

    #列和固定數(shù)值

    select 列名+-*/%數(shù)值 from 表名

    #列和列

    select 列名+-*/%列名 from 表名

**************5、多列進(jìn)行合并為一列查詢

    select concat(列名1,列名2,...)合并后列名 from 表名

**************6、查詢過程增加常量列

    select * ,'常量' 列名 from 表名

5|*0*****條件查詢(where)

5|*1*****1、比較運(yùn)算符作為處理?xiàng)l件

==(=,>,<,>=,<=,!=<>,如果是null,需要寫為is\is not )==

    select * from 表名 where 列名=值

5|*2*****2、多條件

**************1)同時(shí)滿足(and)

    select * from 表名 where 列名1=值 and 列名2=值

**************2)或者(or)

    #不同列

    select * from 表名 where 列名1=值 or 列名2=值

    #同一列的不同值

    select * from 表名 where 列名 in (值1,值2,...)

    select * from 表名 where 列名 not in (值1,值2,...)

**************3)模糊查詢(like)

    #查詢l開頭的名字,任意長(zhǎng)度

    select * from 表名 where 列名 like 'l%'

    #查詢l開頭的名字,固定長(zhǎng)度

    select * from 表名 where 列名 like 'l__'

    #查詢包含o的名字

    select * from 表名 where 列名 like '%o%'

**************4)上下界(between)

    select * from 表名 where 列名 between 下界值 and 上界值

6|*0*****排序查詢(order by)

6|*1*****升序

    select * from 表名 order by 列名

    select * from 表名 order by 列名 asc

6|*2*****降序

    select * from 表名 order by 列名 desc

6|*3*****排序相等行,在進(jìn)行排序

    select * from 表名 order by 列名1 desc,列名2 desc

7|*0*****分頁查詢(limit)

    #起始位置默認(rèn)為0,查詢不包括起始位置

    select * from 表名 limit 起始位置,每頁條目數(shù)

7|*1*****公式

    #假如要顯示的頁數(shù)為page,每一頁條目數(shù)為size

    select 查詢列表

    from 表

    limit (page-1)*size,size;

8|*0*****聚合函數(shù)

關(guān)于某一列進(jìn)行操作,和行沒有關(guān)系

count() 返回結(jié)果集中行的數(shù)目
max() 返回結(jié)果集中所有值的最大值
min() 返回結(jié)果集中所有值的最小值
sum() 返回結(jié)果集中所有值的總和
avg() 返回結(jié)果集中所有值的平均值

8|*1*****用法

聚合函數(shù)null值不參與運(yùn)算,如果希望null值也參與那么需要ifnull()函數(shù)處理

    select 函數(shù)名(列名) from 表名

9|*0*****分組查詢(group by)

    select 分組列名,聚合函數(shù) from 表名 group by 分組列名

    #一般和聚合函數(shù)一起使用

    #如,查詢student表中的男女兩個(gè)分組中的最大年齡和最小年齡

    select ssex,max(sage),min(sage) from student group by ssex

9|*1*****條件篩選(having)

group by后不可以使用where進(jìn)行篩選,需要使用having關(guān)鍵字

如果能用where篩選數(shù)據(jù)的話,絕不使用having

**************where和having的區(qū)別?

  • 都是進(jìn)行條件判斷的關(guān)鍵字
  • where條件判斷是在分組前判斷,having條件判斷是在分組后判斷
  • where關(guān)鍵字要寫在group by前面,having關(guān)鍵字要寫在group by后面
  • where條件里不能寫聚合函數(shù),having條件里可以寫聚合函數(shù)
    #如,查詢年齡大于11的各個(gè)年齡段的人數(shù)

    select sage,count(sage) from student group by sage having sage > 11

    #可以使用where就不要用having

    select sage,count(sage) from student where sage > 11 group by sage

10|*0*****聯(lián)合查詢(union)

  1. 要求多條查詢語句的查詢列數(shù)必須一致
  2. 要求多條查詢語句的查詢的各列類型、順序最好一致
  3. union 去重,union all包含重復(fù)項(xiàng)
    1. 將2個(gè)表的數(shù)據(jù)進(jìn)行拼接,針對(duì)拼接后的重復(fù)數(shù)據(jù) 去重顯示

    select 列名 from 表名1 where 條件

    union 

    select 列名 from 表名2 where 條件

    2. 將2個(gè)表的數(shù)據(jù)進(jìn)行拼接,針對(duì)拼接后的重復(fù)數(shù)據(jù) 不去重顯示

    select 列名 from 表名1 where 條件

    union all

    select 列名 from 表名2 where 條件

11|*0*****多表聯(lián)合查詢

    #sql92

    select * from 表1,表2;

    #sql99

    select * from 表1 cross join 表2;

笛卡爾積組合,形成數(shù)據(jù)沒有價(jià)值

11|*1*****內(nèi)連接(inner join)

    select 列名 from A表 

    inner join B表 on 關(guān)聯(lián)條件;

11|*2*****外連接

**************左外連接(left)

    select 列名 from A表 

    left join B表 on 關(guān)聯(lián)條件;

**************右外連接(right)

    select 列名 from A表 

    right join B表 on 關(guān)聯(lián)條件;

11|*3*****自連接

    #查詢1號(hào)課程成大于2號(hào)課程的學(xué)生id

    select * from sc s1  

    inner join sc s2 

    on s1.sno=s2.sno

    where s1.cno=1 and s2.cno=2 and s1.score>s2.score

11|*4*****去重(distinct)

    select distinct 列名 from 表名;

12|*0*****子查詢

嵌套查詢,就是指一個(gè)sql語句里面還有一條sql語句

將查詢的結(jié)果(可以作為值,也可以作為表),再次被sql語句使用

12|*1*****結(jié)果為一個(gè)值

    #查詢李華老師所帶課程

    select * from course where tno = 

    (

          select tno from teacher where tname = '李華'

    )

12|*2*****結(jié)果為一個(gè)表(多個(gè)值)

    #查詢李華老師所帶學(xué)生的信息

    -- 1 獲取李華老師的編號(hào)

    select tno from teacher where tname = '李華'

    -- 2 根據(jù)老師的編號(hào)獲取所帶學(xué)科的編號(hào)

    select cno from course where cno in 

    (

          select tno from teacher where tname = '李華'

    )

    -- 3 根據(jù)學(xué)科編號(hào)獲取學(xué)生的編號(hào)

    select sno from sc where cno in 

    (

      select cno from course where cno in 

      (

          select tno from teacher where tname = '李華'

      )

    )

    -- 4 根據(jù)學(xué)生編號(hào),獲取學(xué)生的信息

    select * from student where sno in 

    (

      select sno from sc where cno in 

      (

        select cno from course where cno in 

        (

            select tno from teacher where tname = '李華'

        )

      )

    )

12|*3*****any(滿足該字段任何一個(gè)值,‘或’)

    #查詢學(xué)生表中,年齡大于張三或tony的學(xué)生信息

    select * from student where sage > any(

        select sage from student where sname = '張三' or sname = 'tony'

    )

12|*4*****all(滿足該字段所有的值,‘且’)

    #查詢學(xué)生表中,年齡大于張三并且大于tony的學(xué)生信息

    select * from student where sage > all(

        select sage from student where sname = '張三' or sname = 'tony'

    )

12|*5*****exists和in的區(qū)別

13|*0*****擴(kuò)展

13|*1*****按照條件顯示不同信息(case、when、then、else、end)

    #查詢student表中,按照年齡顯示是否成年

    select *,

    case 

      when sage >= 18 then '成年'

        else '未成年'

        end

    from student
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容