1. 程式人生 > >MySQL-查詢資料操作(DQL)

MySQL-查詢資料操作(DQL)

  • SELECT查詢格式說明
SELECT select_expr [,select_expr...]
[
FROM tnl_name
[WHERE 條件]
[GROUP BY {col_name|position} [ASC|DESC], ... 分組]
[HAVING 條件 對分組結果進行二次篩選]
[ORDER BY {col_name|position} [ASC|DESC], ... 排序]
[LIMIT 限制條數]
]

資料庫:cms
資料表:cms_admin cms_cate cms_news cms_user provinces

  • –查詢全部
SELECT * FROM user;
SELECT user.* FROM user;	#查詢表user的所有資料
  • –查詢欄位
SELECT id,username FROM cms_admin;
  • –給表名起別名
SELECT id,username FROM cms_admin AS a;
SELECT id,username FROM cms_admin;

SELECT a.id,a.username,a.email,a.role FROM cms_admin AS a;
  • –給欄位起別名
SELECT a.id AS '編號',username AS '使用者名稱',email AS '郵箱',role AS '角色' FROM cms_admin;
  • –表別名和欄位別名結合起來
SELECT a.id AS i,a.username AS u,a.email AS e,a.role AS r FROM cms_admin AS a;
  • –WHERE條件
  • –指定範圍 BETWEEN AND NOT BETWEEN AND
  • –查詢編號3~10之前的記錄
SELECT * FROM cms_user WHERE id BETWEEB 3 AND 10;
SELECT * FROM cms_user WHERE id NOT BETWEEB 3 AND 10;
  • –指定集合 查詢編號為1,3,5,7,9,11,100的記錄
SELECT * FROM cms_user WHERE id IN(1,3,5,7,9,11,100);
SLEECT * FROM cms_user WHERE proId IN(1,5);
SELECT * FROM cms_user WHERE username IN('zhao','qian','sun');
  • –匹配字元

–模糊查詢:
%:代表0個一個或者多個任意字元;
_:代表1個任意字元;

SELECT * FROM cms_user WHERE username LIKE '%天%';
SELECT * FROM cms_user WHERE username LIKE '張%';
  • –查詢使用者名稱為3位的記錄
SELECT * FROM cms_user WHERE username LIKE '___';

SELECT * FROM cms_user WHERE username LIKE '_I%';
  • –查詢使用者名稱為wang並且密碼為tian的使用者
SELECT * FROM cms_user WHERE username='wang' AND password='tian';
  • –查詢編號大於等於3並且年齡不為NULL的使用者
SELECT * FROM cms_user WHERE id>=3 AND age IS NOT NULL;
  • –查詢編號大於等於3並且年齡不為NULL的使用者並且proId等於3
SELECT * FROM cms_user WHERE id>=3 AND age IS NOT NULL AND proId=3;
  • –查詢編號在5~10且使用者名稱為4位的使用者
SELECT * FROM cms_user WHERE id BETWEEN 5 AND 10 AND username LIKE '____';
  • –查詢使用者名稱以張開頭,或者是使用者所在省份為2,4的記錄
SELECT * FROM cms_user WHERE username LIKE '張%' OR proId IN(2,4);
  • –分組 GROUP BY
SELECT * FROM cms_user GROUP BY proId;
SELECT * FROM cms_user GROUP BY sex;
  • –按照多個欄位分組;先按照proId分,在組內按照sex分
SELECT * FROM cms_user GROUP BY proId,sex;
  • –GROUP BY配合GROUP_CONCAT()得到分組詳情
  • –查詢id,sex,使用者名稱詳情,按照性別分組
SELECT id,sex,GROUP_CONCAT(username) FROM cms_user GROUP BY sex;
  • –查詢proId,性別詳情,註冊時間詳情,使用者名稱詳情,按照proId分組
SELECT proId,GROUP_CONCAT(username),GROUP_CONCAT(sex),GROUP_CONCAT(regTime) FROM cms_user GROUP BY proId;
  • –GROUP BY配合聚合函式COUNT() MAX() MIN() AVG() SUM()
  • –查詢編號,sex,使用者名稱詳情以及組中總人數按照sex分組
SELECT id,sex,GROUP_CONCAT(username) AS users,COUNT(*) AS totalUsers FROM cms_user GROUP BY sex;
  • –統計表中所有記錄
SEELCT COUNT(*) AS totalUsers FROM cms_user;
SEELCT COUNT(id) AS totalUsers FROM cms_user;
  • –COUNT()函式不統計NULL值
SELECT COUNT(age) AS totalUser FROM cms_user;
  • –查詢編號,性別,使用者名稱詳情,組中總人數,組中最大年齡,組中最小年齡,平均年齡,以及年齡總和按照性別分組
SELECT id,sex,
GROUP_CONCAT(username),
COUNT(*) AS totalUsers,
MAX(age) AS max_age,
MIN(age) AS min_age,
AVG(age) AS ave_age,
SUM(age) AS sum_age
FROM cms_user
GROUP BY sex;
  • –GROUP BY配合 WITH ROLLUP 記錄上面所有記錄的總和
SELECT id,sex,
GROUP_CONCAT(username),
COUNT(*) AS totalUsers,
MAX(age) AS max_age,
MIN(age) AS min_age,
AVG(age) AS ave_age,
SUM(age) AS sum_age
FROM cms_user
GROUP BY sex WITH ROLLUP;
  • –HAVING 子句對分組結果進行二次篩選
  • –查詢性別,使用者名稱詳情組中總人數,最大年齡,年齡總和,按照性別分組
SELECT sex,
GROUP_CONCAT(username) AS users,
COUNT(*) AS totalUsers,
MAX(age) AS max_age,
SUM(age) AS sum_age
FROM cms_user
GROUP BY sex;
  • –查詢組中人數大於2的;對上一條sql結果進行二次篩選
SELECT sex,
GROUP_CONCAT(username) AS users,
COUNT(*) AS totalUsers,
MAX(age) AS max_age,
SUM(age) AS sum_age
FROM cms_user
GROUP BY sex
HAVING COUNT(*) > 2;

SELECT sex,
GROUP_CONCAT(username) AS users,
COUNT(*) AS totalUsers,
MAX(age) AS max_age,
SUM(age) AS sum_age
FROM cms_user
GROUP BY sex
HAVING COUNT(*) > 2 AND MAX(age) > 60;

SELECT sex,
GROUP_CONCAT(username) AS users,
COUNT(*) AS totalUsers,
MAX(age) AS max_age,
SUM(age) AS sum_age
FROM cms_user
WHERE id >= 2
GROUP BY sex
HAVING COUNT(*) > 2 AND MAX(age) > 60;
  • –ORDER BY 對查詢結果進行排序

    DESC 降序,ASC 升序

SELECT * FROM cms_user ORDER BY id DESC;
SELECT * FROM cms_user ORDER BY age ASC;
  • –按照年齡升序,id降序排列
SELECT * FROM cms_user ORDER BY age ASC,id DESC;
  • –取隨機數
SELECT * FROM cms_user ORDER BY RAND();
  • –LIMIT 限制查詢結果顯示條數

    LIMIT 顯示條數
    LIMIT 偏移量,顯示條數 #實現分頁顯示

SELECT * FROM cms_user LIMIT 5;

SELECT * FROM cms_user LIMIT 1;
SELECT * FROM cms_user LIMIT 0,1;
  • –總結查詢sql
SELECT id,sex,age,GROUP_CONCAT(username),
COUNT(*) AS totalUsers,
MAX(age) AS max_age,
MIN(age) AS min_age,
AVE(age) AS ave_age,
SUM(age) AS sum_age
FROM cms_user
WHERE id >= 1
GROUP BY sex
HAVING COUNT(*) >= 2
ORDER BY age DESC
LIMIT 0,3;