基本的SELLECT语句
- 列的别名
as:全称:alias(别名),可以省略
列的别名可以使用一对""引起来,不要使用''。
SELECT employee_id emp_id,last_name AS lname,department_id "部门id",salary * 12 AS "annual sal"
FROM employees;
- 去除重复行
查询员工表中一共有哪些部门 id 呢?
SELECT DISTINCT department_id
FROM employees;
SELECT COUNT(DISTINCT department_id)
FROM employees;
- 空值参与运算
空值:null
null 不等同于 0,'','null'
SELECT \* FROM employees; #空值参与运算:结果一定也为空。
SELECT employee_id,salary "月工资",salary _ (1 + commission_pct) _ 12 "年工资",commission_pct
FROM employees; #实际问题的解决方案:引入 IFNULL
SELECT employee_id,salary "月工资",salary _ (1 + IFNULL(commission_pct,0)) _ 12 "年工资",commission_pct
FROM `employees`;
- 查询常数
SELECT '尚硅谷',123,employee_id,last_name
FROM employees;
5.显示表结构
DESCRIBE employees;
DESC employees;
DESC departments;
6.过滤数据
练习:查询 90 号部门的员工信息
SELECT \* FROM employees WHERE department_id=90; #练习:查询 last_name 为'King'的员工信息
SELECT \* FROM employees WHERE last_name='King';