數據庫常用SQL語句


  • 顯示所有的數據庫
    • show databases;
  • 新建數據庫
    • create database if not exists 數據庫名  default character set = 'utf8';
  • 刪除數據庫
    • drop database (if exists) 數據庫名;
  • 使用表
    • use 數據庫名;
  • 新建表
    • create table if not exists 表名(

        id int unsigned primary key auto_increment,

        name varchar(10),

        gender char(1),

        birthday date,

        time datatime,

        score decimal(4,1)

      );

  • 刪除表
    • drop table (if exists) 表名;
  • 增加數據
    • insert into 表名 (字段1, 字段2, 字段3) values

      (字段1值, 字段2值, 字段3值),

      (字段1值, 字段2值, 字段3值);

  • 修改數據
    • update 表名 set 字段1 = ‘字段值1’, 字段2 = ''字段值2 where 條件;
  • 刪除數據
    • delete from 表名 where 條件;                   # 刪除表格里面已有的數據
    • truncate table 表名;                                  # 相當於刪除表后再建一個同名的空表
  • 邏輯刪除(標記)
    • 添加一個字段isdelete,刪除-1,未刪除2
    • update 表名 set isdelete = 1 where id = 1;                # 將id為1的數據的isdelete字段值改為1,即標記為已刪除
    • select * from 表名 where isdelete = 0;                      # 查詢出未被標記刪除的所有數據
  • 查詢
    • select * from 表名;                                                                                                           # 簡單查詢
    • select 字段1 (as) 別名1, 字段2 (as) 別名2 from 表名 where 條件;                                  # 別名
    • select distinct 字段1, 字段2 from 表名 where 條件                                                         # 去重
  • where 條件
    • >  <  =  !=(<>)  <=  >=                                                                                                      # 比較運算

    • and or not                                                                                                                        # 邏輯運算
    • like '孫%'                                                                                                                          # 以孫開頭的任意長度字符(模糊查詢)
    • like '孫_'                                                                                                                           # 以孫開頭的兩個字符(模糊查詢)
    • in (值1, 值2, 值3)                                                                                                             # 范圍查詢,功能同or
    • between 10 and 80                                                                                                         # 范圍查詢,功能同and
    • is (not) null                                                                                                                      # (不)為空,null表示未填寫
  • 排序
    • order by 字段1 desc/asc, 字段2 desc/asc                                                                      # desc (降序)、asc(升序,默認)
    • order by convert(字段名 using gbk)                                                                               # 將某個字段按照中文排序
  • 聚合函數
    • select count(*) from 表名;                                                                                              # 計數, ps:為null的數據不會被統計進去
    • select max(字段名) from 表名;                                                                                      # 最大值
    • select min(字段名) from 表名;                                                                                       # 最小值
    • select avg(字段名) from 表名;                                                                                       # 平均值
    • select sum(字段名) from 表名;                                                                                      # 求和
  • 分組
    • group by 字段名                                                                                                            # 按照某字段進行分組,有去重的作用,一般與聚合函數搭配使用
    • 所有select的字段,除聚合函數中的字段,都必須在group by中出現。只要滿足這個規則就可以 
  • 分組后過濾
    • select count(*) from 表名 where sex = '男';                                                                  # 查詢所有男生的人數(方法1---先過濾,在聚合)
    • select count(*) from 表名 group by sex having sex = '男';                                            # 查詢所有男生的人數(方法2---先分組聚合再過濾)
  • 分頁
    • select * from 表名 limit 2;                                                                                          # 查詢前2條數據,相當於limit  0, 7
    • select * from 表名 limit 2, 3;                                                                                        # 跳過前2條,查詢其后的3條
    • select * from 表名 limit (頁數-1) * 每頁顯示的條數, 每頁顯示的條數;                         # 分頁功能
  • 連接查詢(多表查詢/等值連接)
    • select * from 表1, 表2;                                                                                                # 多表查詢,但是會造成 “笛卡爾積”
    • select * from 表1, 表2 where 表1.字段名 = 表2.字段名;                                             # 等值連接進行多表查詢,先連接另一張表的數據(必然會造成笛卡爾積,產生表1數據 * 表2數據的臨時表),再進行where 的過濾判斷
  • 連接查詢(內連接)
    • select * from 表1 inner join 表2 on 表1.字段名 = 表2.字段名;                                    # 連接的過程中先判斷on條件,再連接另一張表的數據(不會產生臨時表,不會生成笛卡爾積,性能高)
  • 連接查詢(左連接)
    • select * from 表1 left join 表2 on 表1.字段名 = 表2.字段名;                                       # 以左邊表(表1)的數據為准,即顯示全部的表1數據
  • 連接查詢(右連接)
    • select * from 表1 right join 表2 on 表1.字段名 = 表2.字段名;                                     # 以右邊表(表2)的數據為准,即顯示全部的表2數據
  • 自關聯
    • select * from 表1 s1, 表1 s2 where s1.字段名 = s2.字段名;                                       # 一張表需要用到多次
  • 子查詢
    • 標量子查詢(子查詢返回的結果是一行一列(一個值))
      • select * from student where age >
        (select avg(age) from student);                    # 查詢出所有年齡大於平均年齡的學生信息

    • 列子查詢(子查詢返回的結果是多行一列(多個值))
      • select score from 成績表 where sid in (select sid from 學生表 where age = 18);    # 查詢出所有年齡為18歲的學生的成績

    • 行子查詢(子查詢返回的結果是一行多列)
      • select * from student where (sex, age) = (select sex, age from student where sex = '男' order by age desc limit 1);     # 查詢出男生中年齡最大的學生的信息

    • 表子查詢(子查詢返回的結果是多行多列(臨時表))
      • select cname, score from 成績表
        inner join
        (select * from 課程表 where cname = ('數據庫', '系統測試')) as c on 成績表.課程編號 = c.課程編號;                     # 查詢數據庫和系統測試課程的成績

    • 關鍵字
      • in ----------------- 范圍
        • select score from 成績表 where sid in (select sid from 學生表 where age = 18);    # 查詢出所有年齡為18歲的學生的成績
      • (> < =)any/some  () ------------------ =any/some 相當於 in ,>any/some 大於最小的,<any/some 小於最大的
      • (> < !=)all ()              ----------------- !=all 相當於not in ,>all 大於最大的,<all小於最小的


免責聲明!

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



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