MYSQL-分組查詢-where和having的區別


-- DQL查詢操作
-- 查詢全部字段
select * from 表名;

-- 按照條件查詢全部字段
select * from 表名 where 篩選條件

-- 查詢指定字段的數據
select 列名,列名1 from 表名 where 篩選條件;

-- 分組查詢as起別名,如未分組統計查詢全部結果
select 組列表,count() as 別名 from 表名 where 查詢表的篩選條件 group by
分組的字段 having 分組置灰的結果篩選

-- 聚合函數
count(*|字段名)-- *統計數據個數 字段名統計字段下部位null值得數據個數
-- 求和 sum(字段名)
select sum(求和字段) from 表名;
select deptno,SUM(esal) as 部門求和 from emp group by deptno;

-- AVG求平均值
select avg(esal) from emp;
SELECT deptno,avg(esal) as 部門平均 from emp group by deptno;

-- max查詢結果中最大值
SELECT deptno,max(esal) as 部門最大 from emp group by deptno;

-- min查詢結果最小值
SELECT deptno,min(esal) as 部門最小 from emp group by deptno;

-- 在分組的情況下 列出指定字段數據GROUP_CONCAT
-- 列出指定姓名
SELECT deptno,GROUP_CONCAT(ename) as 部門最小 from emp group by deptno;

-- where和having的區別
-- where后面的篩選條件篩選表中數據
-- having對分組后進行篩選
-- 對大於7000的員工分組部門篩選並列出對應員工姓名
SELECT deptno,GROUP_CONCAT(ename) from emp where esal >7000 group by deptno;

-- 在以上的情況下 篩選出部門大於1的
select deptno,GROUP_CONCAT(ename) from emp where esal > 7000 group by deptno;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM