資料庫常用查詢陳述句(DQL)
基本查詢
select 欄位1, 欄位2,…from 表名;
例如:select id , name from stu;
條件查詢
select 欄位1, 欄位2,…from 表名 where 欄位 關系符號 值 ;
關系符號
< = >= <= != 大于 等于 大于等于 小于等于 不等于
例如:select * from stu id > 2;
and or in(范圍內滿足in內部條件) not in 相反
例如: select * from stu where id>1 and age <40;
? select * from stu where id >1 or name =‘張三’;
? select * from stu where id in(3,4);
between 值1 and 值2 在[值1,值2]之間 包含兩邊臨界值
例如: select * from stu where id between 2 and 4;
模糊查詢
select * from 表名 where 欄位 like ‘%值%’;
例如: select * from stu where name like ‘陳%’;
注: %的位置不同 表達的意思不同 陳% : 陳某某 ,%陳% : 某陳某,%陳:某某陳
% 匹配任意字符 (%可以是任意長度)_匹配指定長度字符 一個_代表一個長度
排序查詢
select * from 表名 order by 欄位 排序型別 asc 升序 desc 降序 沒寫排序型別 默認 升序
例: select * from stu order by id desc ;
聚合函式 多行資料一行回傳
count(欄位) 計數 計算該列不為空的資料個數
例 :select count(name) from stu;
sum(欄位) 求和 計算該列所有數字的和 字串求和結果為0
例:select sum(age) from stu;
max(欄位) 最大值 獲取該列最大值
例: select max(age) from stu;
min(欄位) 最小值 獲取該列最小值
例: select min(age) from stu;
avg(欄位) 平均值 不為null的進行平均
例: select avg(age) from stu;
注:聚合函式要放在select 和 from 之間
去重
distinct(列) 一般配合count()一起使用
分組查詢
select * from stu group by 欄位
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/246234.html
標籤:其他
