SQL 常用基本語句總結大全—增刪改查語句

 

SQL 語句的添加、刪除、修改

 

添加、刪除、修改使用 db.Execute(Sql)命令執行操作

 

() Select 語句

 

  1. 普通查詢

 

  1. 查詢全部的行和列

select * from users

 

  1. 查詢部分的行並用 as 命名列(as 可用空格代替)

select UserName as 用戶名,NickName 密碼,sex from users where sex = 0 select UserName,NickName,sex from users where sex <> 0

  1. 查詢空行
    1. select UserName from users where sex is null;
    2. select UserName from users where sex = "";
  2. 使用常量列

select UserName as  用戶名,"中北大學" as "學校" from users;

  1. 限制固定行數

select UserId ,UserName as 用戶名 from users limit 2,4;

  1. 升序排列 降序排列 按照多列排序
    1. select * from users where Height >178 order by age asc;
    2. select * from users where Height >178 order by age desc;
    3. select UserName as 用戶名,Height  身高,Age as 年齡 from users where Age>20 order by Height asc, Age desc;

 

  1. 隨機查詢前 20 條數據

Select * from users order by rand () limit 20;

  1. 關鍵字順序:

Where------group by-----having order by

 

  1. 高級查詢:

 

  1. 模糊查詢——like

select SName as 姓名 from Students where Sname like ‘張%’;

  1. 模糊查詢——IS NULL

select Sname as 姓名,SAddress as 地址 form Students where SAddress is null

 

  1. 模糊查詢——between

select StudentId,Score from Score where Score between 60 and 80

  1. 模糊查詢——in

select Sname as 學員姓名,SAddress as 地址 from Students where SAddress in(‘北京’,’廣州’,’上海’);

 

  1. 聚合函數

 

  1. 分組查詢

 

分組查詢——GROUP BY

 

    1.  

分組查詢——多列分組

分組查詢——HAVING

 

 

    1. 分組查詢總結
      1. where 字句從數據源中去掉不符合其搜索條件的數據
      2. group by 字句蒐集數據航到各個組中
      3. 統計函數爲各個組計算統計值
      4. having 字句去掉不符合其搜索條件的各組數據行
      5. 使用 group by 時,select 後面出現的內容要麼爲聚合函數, 要麼爲 group by 後面出現的內容
      6. 關鍵字的先後順序:where——group by——having——order by

 

 

多表連接查詢

分類:

內連接(inner join):(等值連接、內連接)只返回兩個表中連結字段相等的行外連接:

  1. 左外連接(left join)返回包括左表中的所有記錄和右表中連結字段相等的記錄
  2. 右連接(right join):返回包括右表中的所有記錄和左表中連結字段相等的記錄。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章