1. 程式人生 > >mysql_表內容_操作

mysql_表內容_操作

BE 大數 分享圖片 查詢 fse 圖片 and esc 之間

1、增

語法:insert into 表 (列名,列名...) values (值,值...)

# 插入單條數據
        insert into 表 (列名,列名...) values (值,值...)

技術分享圖片

# 插入多條數據
       insert into 表 (列名,列名...) values (值,值...),(值,值...)

技術分享圖片

# 插入另一條語句的查詢結果
        insert into 表 (列名,列名...) select 列名,... from

技術分享圖片

技術分享圖片

2、刪

語法:delete from 表

delete from 表;
delete from
where id=1;

技術分享圖片

3、改

語法:update 表 set name = ‘nick‘ where id>1

update 表 set name = nick where id>1

技術分享圖片

4、查

語法:select * from 表

select * fromselect * fromwhere id > 1
select nid,name,gender as gg fromwhere id > 1

# as 做別名

技術分享圖片

5、條件

語法:select * from 表 where id > 1

    select * from
where id > 1 and name != nick and num = 12; # 多個條件 select * fromwhere id between 5 and 16; # id在5到16之間 select * fromwhere id in (11,22,33); # id在元祖中 select * fromwhere id not in (11,22,33); # id不在元祖中
select * fromwhere id in (select nid from 表); # id在查詢結果中

技術分享圖片

6、通配符

語法:select * from 表 where name like ‘_n%‘

  select * fromwhere name like ni%  # ni開頭的所有(多個字符串)
    select * fromwhere name like s_   # s開頭的所有(一個字符)

7、限制

語法:select * from 表 limit 9,5;

    select * from 表 limit 5;            # 前5行
    select * from 表 limit 9,5;          # 從第9行開始的5行
    select * from 表 limit 5 offset 9    # 從第9行開始的5行

技術分享圖片

8、排序

語法:select * from 表 order by 列1 desc,列2 asc

    select * from 表 order by 列 asc             # 根據 “列” 從小到大排列
    select * from 表 order by 列 desc            # 根據 “列” 從大到小排列
    select * from 表 order by 列1 desc,列2 asc   # 根據 “列1” 從大到小排列,如果相同則按列2從小到大排序

技術分享圖片

9、分組

語法:select num from 表 group by num

    select num from 表 group by num           # 根據num分組
    select num,nid from 表 group by num,nid   # 根據num和nid分組
    select num,nid from 表  where nid > 10 group by num,nid order nid desc
    select num,nid,count(*),sum(score),max(score),min(score) from 表 group by num,nid  # 內置函數
    select num from 表 group by num having max(id) > 10    # 前面計算的結果交由後面處理
 
    註:group by 必須在where之後,order by之前
count(*)、count(1) # 表示個數
sum(score)        # 表示和
max(score)        # 表示最大數
min(score)        # 表示最小數

having            # 要用前面處理結果是用having。

10、連表

語法:inner join . onleft join . onright join . on

    無對應關系則不顯示
    select A.num, A.name, B.name
    from A,B
    Where A.nid = B.nid
 
    無對應關系則不顯示
    select A.num, A.name, B.name
    from A inner join B
    on A.nid = B.nid
 
    A表所有顯示,如果B中無對應關系,則值為null
    select A.num, A.name, B.name
    from A left join B
    on A.nid = B.nid
 
    B表所有顯示,如果B中無對應關系,則值為null
    select A.num, A.name, B.name
    from A right join B
    on A.nid = B.nid

11、組合

語法:union、union all

    組合,自動處理重合
    select nickname
    from A
    union
    select name
    from B
 
    組合,不處理重合
    select nickname
    from A
    union all
    select name
    from B

參考:https://www.cnblogs.com/suoning/articles/5769141.html

mysql_表內容_操作