CREATE DATABASE dbtest2;
SHOW CREATE DATABASE dbtest2;
USE dbtest2;
CREATE TABLE emp(id INT, lname VARCHAR(20));
SHOW CREATE TABLE emp;
SELECT * FROM emp;
DESCRIBE emp;
DESC emp;
SELECT 1; # 1
SELECT 9/2; # 4.5
SELECT 1, 9/2 FROM DUAL; # DUAL:伪表
SELECT * FROM departments; # * 表示表中所有的列
SELECT department_name, location_id FROM departments; # 查询指定列
列的别名尽量使用""
引起来,不要使用单引号''
。
SELECT employee_id AS emp_id, last_name lname, department_id "部门id", salary * 12 AS 'annual sal' FROM employees;
SELECT DISTINCT department_id FROM employees;
DISTINCT
需要放到所有列名的前面。
SELECT salary, DISTINCT department_id FROM employees; # 报错
DISTINCT
是对后面所有列名的组合进行去重,以下是对department_id, salary两字段的组合去重
SELECT DISTINCT department_id, salary FROM employees; # 没报错,但无意义
空值:null
,不等同于0
,''
,'null'
,遇到null
,运算结果即为null
。
SELECT employee_id, salary AS "月薪", salary * (1 + commission_pct) * 12 AS "年薪", commission_pct FROM employees;
实际解决问题方案:引入IFNULL
。
SELECT employee_id, salary AS "月薪", salary * (1 + IFNULL(commission_pct, 0)) * 12 AS "年薪", commission_pct FROM employees;
USE dbtest2;
CREATE TABLE ORDER(id INT); # 报错
CREATE TABLE `order`(id INT);
USE atguigudb;
SELECT "你好", 123, employee_id, last_name FROM employees;
DESCRIBE employees;
DESC departments;
# 查询90号部门的员工信息
SELECT * FROM employees WHERE department_id = 90;
# 查询部门为null的员工信息
# 使用 IS NULL
SELECT * FROM employees WHERE department_id IS NULL;
# 使用<=>
SELECT * FROM employees WHERE department_id <=> NULL;
尚硅谷宋红康MySQL