题目描述
查找employees表所有emp_no为奇数,且last_name不为Mary(注意大小写)的员工信息,并按照hire_date逆序排列(题目不能使用mod函数)
CREATE TABLE `employees` (
`emp_no` int(11) NOT NULL,
`birth_date` date NOT NULL,
`first_name` varchar(14) NOT NULL,
`last_name` varchar(16) NOT NULL,
`gender` char(1) NOT NULL,
`hire_date` date NOT NULL,
PRIMARY KEY (`emp_no`));
解答:
取余判断奇偶
select * from employees e
where e.emp_no%2=1
and last_name!="Mary"
order by hire_date desc
位运算判断奇偶:
- x & 1 = 1 ,则x为奇数
- x>>1<<1(x先除2再乘2) ,如果等于x,则x为偶数
select *
from employees e
where e.emp_no & 1 = 1
and e.last_name != 'Mary'
order by e.hire_date desc