我們先把數據表建好
| 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