-- 示例代码:使用 COUNT 函数结合条件查询
-- 假设有一个名为 'employees' 的表,包含以下字段:
-- id (员工ID), name (员工姓名), department (部门), salary (薪水)
-- 1. 统计某个部门的员工数量
SELECT COUNT(*) AS employee_count
FROM employees
WHERE department = 'Sales';
-- 解释:这条 SQL 语句会统计 'employees' 表中 'department' 字段等于 'Sales' 的记录数,并将结果命名为 'employee_count'。
-- 2. 统计薪水大于 5000 的员工数量
SELECT COUNT(*) AS high_salary_employees
FROM employees
WHERE salary > 5000;
-- 解释:这条 SQL 语句会统计 'employees' 表中 'salary' 字段大于 5000 的记录数,并将结果命名为 'high_salary_employees'。
-- 3. 统计特定条件下非空字段的数量
SELECT COUNT(salary) AS non_null_salaries
FROM employees
WHERE department = 'HR';
-- 解释:这条 SQL 语句会统计 'employees' 表中 'department' 字段等于 'HR' 且 'salary' 字段不为空的记录数,并将结果命名为 'non_null_salaries'。
上一篇:mysql 查看参数
下一篇:mysql id自增
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站