欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MySQL之復(fù)雜查詢的實現(xiàn)

 更新時間:2022年02月18日 10:54:39   作者:yzm4399  
本文主要介紹了MySQL之復(fù)雜查詢的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

1.排序

ORDER BY 子句來設(shè)定哪個字段哪種方式來進(jìn)行排序,再返回搜索結(jié)果。
desc:降序

select * from blog order by balance desc;

asc:升序,默認(rèn),可不寫

select * from blog order by balance asc;

在這里插入圖片描述

多字段排序

update blog set age = 25 where age < 25;

先根據(jù)年齡升序,再根據(jù)余額降序

select * from blog order by age asc, balance desc;

在這里插入圖片描述

2.分組

GROUP BY 語句根據(jù)一個或多個列對結(jié)果集進(jìn)行分組。
新建員工表

CREATE TABLE `employee` (
  `id` int NOT NULL,
  `name` varchar(20) NOT NULL DEFAULT '',
  `identity` varchar(20) NOT NULL DEFAULT '',
  `signin` tinyint NOT NULL DEFAULT '0' COMMENT '打卡次數(shù)',
  `date` date NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;

插入數(shù)據(jù)

INSERT INTO `employee` VALUES 
('1', '小明', 'firemen', 3, '2022-02-21'), 
('2', '小王', 'doctor', 3, '2022-02-21'), 
('3', '小麗', 'police', 3, '2022-02-21'), 
('4', '小王', 'doctor', 2, '2022-02-22'), 
('5', '小明', 'firemen', 1, '2022-02-22'), 
('6', '小麗', 'police', 3, '2022-02-22'), 
('7', '小明', 'firemen', 2, '2022-02-23'),
('8', '小王', 'doctor', 2, '2022-02-23'),
('9', '小紅', 'nurse', 3, '2022-02-23');

在這里插入圖片描述

統(tǒng)計每人打卡記錄數(shù)

SELECT name, COUNT(*) FROM employee GROUP BY name;

在這里插入圖片描述

WITH ROLLUP 可以實現(xiàn)在分組統(tǒng)計數(shù)據(jù)基礎(chǔ)上再進(jìn)行相同的統(tǒng)計(SUM,AVG,COUNT…)
統(tǒng)計每人打卡總數(shù)

SELECT name, SUM(signin) as signin_count FROM employee GROUP BY name WITH ROLLUP;

在這里插入圖片描述

其中記錄 NULL 表示所有人的登錄次數(shù)。
使用 coalesce 來設(shè)置一個可以取代 NUll 的名稱
coalesce 語法:select coalesce(a,b,c);

SELECT name, SUM(signin) as signin_count FROM employee GROUP BY name WITH ROLLUP;

在這里插入圖片描述

3.聯(lián)合查詢

UNION 操作符用于連接兩個以上的 SELECT 語句的結(jié)果組合到一個結(jié)果集合中。
UNION ALL:返回所有結(jié)果集,包含重復(fù)數(shù)據(jù)。

select author from blog 
UNION ALL 
select identity from employee;

在這里插入圖片描述

報錯:Illegal mix of collations for operation ‘UNION’
原因:相同字段的編碼不一致造成的

在這里插入圖片描述

解決:修改blog表的author字段

alter table blog modify `author` varchar(40) COLLATE utf8mb4_general_ci NOT NULL DEFAULT '';

在這里插入圖片描述

在這里插入圖片描述

UNION DISTINCT: 刪除結(jié)果集中重復(fù)的數(shù)據(jù)。默認(rèn),可不寫

select author from blog 
UNION
select identity from employee;

4.多表連接

INNER JOIN(內(nèi)連接,或等值連接):獲取兩個表中字段匹配關(guān)系的記錄。
LEFT JOIN(左連接):獲取左表所有記錄,即使右表沒有對應(yīng)匹配的記錄。
RIGHT JOIN(右連接): 與 LEFT JOIN 相反,用于獲取右表所有記錄,即使左表沒有對應(yīng)匹配的記錄。

內(nèi)連接
INNER 可以省略

select b.author, b.age, b.title, e.name, e.signin from blog b 
INNER JOIN employee e on b.author = e.identity;

在這里插入圖片描述

where條件實現(xiàn)內(nèi)連接效果

select b.author, b.age, b.title, e.name, e.signin from blog b,employee e 
where b.author = e.identity;

在這里插入圖片描述

左連接:讀取左邊數(shù)據(jù)表的全部數(shù)據(jù),即便右邊表無對應(yīng)數(shù)據(jù)。

select b.author, b.age, b.title, e.name, e.signin from blog b 
LEFT JOIN employee e on b.author = e.identity;

右連接:讀取右邊數(shù)據(jù)表的全部數(shù)據(jù),即便左邊邊表無對應(yīng)數(shù)據(jù)。

select b.author, b.age, b.title, e.name, e.signin from blog b 
RIGHT JOIN employee e on b.author = e.identity;

在這里插入圖片描述

 到此這篇關(guān)于MySQL之復(fù)雜查詢的實現(xiàn)的文章就介紹到這了,更多相關(guān)MySQL 復(fù)雜查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論