文章目录
- 比较运算符
-
- < > = !
- IS NULL \ IS NOT NULL \ ISNULL
- LEAST() \ GREATEST() 查询数据大小(字典序)
- BETWEEN...AND...
- IN (SET) \ NOT IN (SET)
- LIKE 模糊查询
- REGEXP \ RLIKE
- 逻辑运算符
-
- 逻辑运算符: OR (||)、AND (&&)、 NOT (!)、 XOR
- 位运算符
-
- 排序
-
- 分页
-
比较运算符
< > = !
SELECT 1 =1, 1 != 2, 1 = '1', 1 = 'a', 0 = 'a'
FROM DUAL;
SELECT 'a' = 'a', 'ab' = 'ab', 'a' = 'b'
FROM DUAL;
SELECT 1 = NULL, NULL = NULL
FROM DUAL;
SELECT 1 <=> NULL, NULL <=> NULL
FROM DUAL;
IS NULL \ IS NOT NULL \ ISNULL
SELECT last_name,salary,commission_pct
FROM employees
WHERE commission_pct IS NULL;
SELECT last_name,salary,commission_pct
FROM employees
WHERE commission_pct IS NOT NULL;
SELECT last_name,salary,commission_pct
FROM employees
WHERE ISNULL(commission_pct);
LEAST() \ GREATEST() 查询数据大小(字典序)
SELECT LEAST('g','b','c','a'), GREATEST('g','b','c','a')
FROM DUAL;
SELECT LEAST(first_name,last_name), GREATEST(first_name,last_name)
FROM employees;
BETWEEN…AND…
SELECT employee_id,last_name,salary
FROM employees
WHERE salary BETWEEN 6000 AND 8000;
IN (SET) \ NOT IN (SET)
SELECT last_name,salary,department_id
FROM employees
WHERE department_id IN(10,20,30);
SELECT last_name,salary,department_id
FROM employees
WHERE department_id NOT IN (10,20,30);
LIKE 模糊查询
SELECT last_name
FROM employees
WHERE last_name LIKE '%a%';
SELECT last_name
FROM employees
WHERE last_name LIKE 'a%';
SELECT last_name
FROM employees
WHERE last_name LIKE '%a%' AND last_name LIKE '%e%';
SELECT last_name
FROM employees
WHERE last_name LIKE '_a%';
SELECT last_name
FROM employees
WHERE last_name LIKE '_\_a%';
REGEXP \ RLIKE
逻辑运算符
逻辑运算符: OR (||)、AND (&&)、 NOT (!)、 XOR
SELECT last_name,salary,department_id
FROM employees
WHERE department_id = 50 AND salary > 6000;
SELECT last_name,salary,department_id
FROM employees
WHERE NOT commission_pct <=> NULL;
SELECT last_name,salary,department_id
FROM employees
WHERE department_id = 50 XOR salary > 6000;
位运算符
位运算符 & | ^ ~ >> <<
SELECT 12 & 5 ,12 | 5,12 ^ 5
FROM DUAL;
SELECT 10 & ~1
FROM DUAL;
SELECT 4 << 1, 8 >> 1
FROM DUAL;
排序
ORDER BY
SELECT employee_id,last_name,salary
FROM employees
ORDER BY salary ASC;
SELECT employee_id,last_name,salary
FROM employees
ORDER BY salary DESC;
SELECT employee_id,salary,salary * 12 annual_sal
FROM employees
ORDER BY annual_sal;
SELECT employee_id,salary
FROM employees
WHERE department_id IN (50,60,70)
ORDER BY department_id DESC;
SELECT employee_id,salary,department_id
FROM employees
ORDER BY department_id DESC, salary ASC;
分页
使用limit实现数据的分页显示
SELECT employee_id,last_name
FROM employees
LIMIT 0,20;
SELECT employee_id,last_name,salary
FROM employees
WHERE salary > 6000
ORDER BY salary
LIMIT 0,20;
SELECT employee_id,last_name
FROM employees
LIMIT 31,2;
SELECT employee_id,last_name
FROM employees
LIMIT 2 OFFSET 31;
SELECT *
FROM employees
ORDER BY salary DESC
LIMIT 1;