MySQL Query to Filter Employees Based on Name and ID
This query aims to select employee information based on name patterns and ID divisibility.
Query:
select employee_id, salary + bonus
from employees
where name not like 'M%' and employee_id % 2 = 1
union
select employee_id, salary * 0 + bonus
from employees
where name like 'M%' or employee_id % 2 = 1
group by employee_id
Explanation:
select employee_id, salary + bonus: Selects the employee ID and the sum of salary and bonus.from employees: Specifies the table to retrieve data from.where name not like 'M%' and employee_id % 2 = 1: Filters for employees whose name does not start with 'M' and whose employee ID is odd.union: Combines the results of the two SELECT statements.select employee_id, salary * 0 + bonus: Selects the employee ID and the bonus amount (salary * 0 + bonus equals bonus).where name like 'M%' or employee_id % 2 = 1: Filters for employees whose name starts with 'M' or whose employee ID is odd.group by employee_id: Groups the results by employee ID.
Note: This query assumes the existence of a table named 'employees' with columns 'employee_id', 'salary', and 'bonus'. The specific logic and conditions can be adjusted based on your data and requirements.
原文地址: https://www.cveoy.top/t/topic/mzFV 著作权归作者所有。请勿转载和采集!