我们先把数据表建好
use test;create table `employee`( emp_no int unsigned, emp_name varchar(30), emp_sex varchar(3), emp_age tinyint unsigned, sal double, history datetime);insert into employee values(1, '张三', '男', 18, 5000, '2012-04-23'),(2, '李四', '男', 27, 4500, '2013-05-23'),(3, '王五', '男', 23, 4700, '2012-04-21'),(4, '子龙', '男', 19, 3800, '2011-03-04'),(5, '李白', '男', 15, 6200, '2015-09-09'),(6, '刘备', '男', 28, 2500, '2016-02-11'),(7, '吕布', '男', 21, 6000, '2010-10-18'),(8, '尚香', '女', 16, 4500, '2011-09-26'),(9, '小乔', '女', 15, null, '2013-07-05'),(10, '大乔', '女', 16, 5000, '2017-09-01'); |
常用的运算符:
1: 等于( = )
select * from employee where sal = 3800; select * from employee where sal = null; --这里查询不到为null的数据 |
2: 等于( <=> )
select * from employee where sal <=> 3800; select * from employee where sal <=> null; --这里可以查询到为null的数据 |
3: is判断(null)
select * from employee where sal is null; select * from employee where sal is not null; |
4: null值判断还可以使用isnull();
select * from employee where isnull(sal); select * from employee where !isnull(sal); |
5: 在区间(between)内 between min and max ps:这里是一个闭区间
select * from employee where sal between 4500 and 5000;
6: 不在区间内
select * from employee where sal not between 4500 and 5000;
--null不为包括进去
7: and 和 or
select * from employee where sal not between 4500 and 5000 or sal is null; select * from employee where sal = 4500 and emp_sex = '女'; |
8: 小于(<), 大于(>), 小于等于(<=), 大于等于(>=)
select * from employee where sal >= 4500;
***************************************************************************************************************
数学函数
1: rand();
select rand() from dual; --dual是一个伪表 select 1+1 from dual; select rand(); --可以简写 |
2: least(value1, value2, ...) 返回最小值
select least(54,76,4,65,76,87,87,56,65,654,45,23,1,76); select least(54,76,4,65,76,87,87,56,65,654,45,23,1,76) as min_value; --列名可以起一个别名 |
3: greatest(value1, value2, ...) 返回最大值
select greatest(54,76,4,65,76,87,87,56,65,654,45,23,1,76);
4: round(M, D); 返回M的四舍五入的值, D表示要保留几们小数,默认值是0