SQL 語句的添加、刪除、修改
添加、刪除、修改使用 db.Execute(Sql)命令執行操作
(一) Select 語句
- 普通查詢
- 查詢全部的行和列
select * from users
- 查詢部分的行並用 as 命名列(as 可用空格代替)
select UserName as 用戶名,NickName 密碼,sex from users where sex = 0 select UserName,NickName,sex from users where sex <> 0
- 查詢空行
- select UserName from users where sex is null;
- select UserName from users where sex = "";
- 使用常量列
select UserName as 用戶名,"中北大學" as "學校" from users;
- 限制固定行數
select UserId ,UserName as 用戶名 from users limit 2,4;
- 升序排列 降序排列 按照多列排序
- select * from users where Height >178 order by age asc;
- select * from users where Height >178 order by age desc;
- select UserName as 用戶名,Height 身高,Age as 年齡 from users where Age>20 order by Height asc, Age desc;
- 隨機查詢前 20 條數據
Select * from users order by rand () limit 20;
- 關鍵字順序:
Where------group by-----having order by
- 高級查詢:
- 模糊查詢——like
select SName as 姓名 from Students where Sname like ‘張%’;
- 模糊查詢——IS NULL
select Sname as 姓名,SAddress as 地址 form Students where SAddress is null
- 模糊查詢——between
select StudentId,Score from Score where Score between 60 and 80
- 模糊查詢——in
select Sname as 學員姓名,SAddress as 地址 from Students where SAddress in(‘北京’,’廣州’,’上海’);
- 聚合函數
- 分組查詢
分組查詢——GROUP BY
分組查詢——多列分組
分組查詢——HAVING
-
- 分組查詢總結
- where 字句從數據源中去掉不符合其搜索條件的數據
- group by 字句蒐集數據航到各個組中
- 統計函數爲各個組計算統計值
- having 字句去掉不符合其搜索條件的各組數據行
- 使用 group by 時,select 後面出現的內容要麼爲聚合函數, 要麼爲 group by 後面出現的內容
- 關鍵字的先後順序:where——group by——having——order by
- 分組查詢總結
多表連接查詢
分類:
內連接(inner join):(等值連接、內連接)只返回兩個表中連結字段相等的行外連接:
- 左外連接(left join)返回包括左表中的所有記錄和右表中連結字段相等的記錄
- 右連接(right join):返回包括右表中的所有記錄和左表中連結字段相等的記錄。