Basic SELECT statement

SELECT …

SELECT 1;  -- 返回1
SELECT 1+1;-- 返回2
SELECT 1*3;-- 返回3

SELECT … FROM

  • DUALIt is a fake table.
  • SELECT 字段1,字段2,...字段n FROM 表名
  • SELECT * FROM 表名. Which *represents all fields (or all columns) in the table.
SELECT 1
FROM DUAL;
-- 其中,DUAL是伪表。

DESCRIBE DUAL;
-- 报错:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DUAL' at line 1
SELECT *
FROM employees;
-- *,代表 表中的所有字段(或者所有列)

SELECT DISTINCT ... (remove duplicate rows)

  • SELECT DISTINCT, to remove duplicate rows.
  • SELECT DISTINCT department_id, to remove department_idduplicate rows.
  • SELECT DISTINCT department_id,salary, to remove department_id, salarywhile repeating rows.
SELECT department_id
FROM employees; 
-- 返回107条记录

# 去除重复行
SELECT DISTINCT department_id
FROM employees;
-- 返回12条记录
# 反面案例
SELECT DISTINCT department_id
FROM employees; 
-- 返回12行记录

SELECT salary,department_id
FROM employees;
-- 返回106行记录

SELECT salary,DISTINCT department_id
FROM employees;
-- 报错
/*
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DISTINCT department_id
FROM employees' at line 1
*/
# 反面案例
SELECT department_id,salary
FROM employees;
-- 返回107行记录

SELECT DISTINCT department_id,salary
FROM employees;
-- 返回74行记录

SELECT department_id,salary
FROM employees
WHERE department_id = 90 
AND salary = 17000;
/*
返回2行记录,如下:
department_id salary
90	          17000.00
90	          17000.00
*/

SELECT DISTINCT department_id,salary
FROM employees
WHERE department_id = 90
AND salary = 17000;
/*
返回1行记录,如下,
department_id salary
90	          17000.00
即 SELECT DISTINCT department_id,salary  剔除了 department_id、salary同时重复的记录。
*/

column alias

There are three ways to alias a column,

  1. Column name aliases, such asSELECT employee_id emp_id FROM employees;
  2. Column name AS alias, such asSELECT employee_id AS emp_id FROM employees;
  3. Column name "alias", such asSELECT employee_id "emp_id" FROM employees;
SELECT employee_id emp_id 
FROM employees;

SELECT department_name AS dep_name
FROM departments;

SELECT salary*12 "annual salary"
FROM employees;

Filter data using WHERE

SELECT … FROM … WHERE

  1. Query the employee information whose department_id is equal to 90.
    SELECT * FROM employees WHERE department_id = 90;
  2. Query last_name is 'King' employee information
    SELECT * FROM employees WHERE last_name = 'King';
    SELECT * FROM employees WHERE last_name = 'king';
    SELECT * FROM employees WHERE LAST_NAME = 'KING';
    MySQL is not case-sensitive.
# 查询department_id等于90的员工信息。
SELECT *
FROM employees
WHERE department_id = 90;

# 查询last_name为'King'员工信息
SELECT *
FROM employees
WHERE last_name = 'King';

SELECT *
FROM employees
WHERE last_name = 'king';

SELECT *
FROM employees
WHERE LAST_NAME = 'KING';

Column aliases cannot be used in WHERE

SELECT last_name,salary,12*salary*(1+IFNULL(commission_pct,0)) annual_salary
FROM employees
WHERE annual_salary > 100000;
-- 报错:Unknown column 'annual_salary' in 'where clause'

The above example reports an error because the execution sequence of MySQL is,
step 1: FROM.
Step 2: WHERE.
Step 3: SELECT.

Null values ​​participate in operations

Null value, ie NULL. Null values ​​participate in the operation, and the result is still a null value.

SELECT employee_id,salary "月工资",commission_pct,12*salary*(1+commission_pct) "年收入"
FROM employees;
-- 当commission_pct为NULL时,12*salary*(1+commission_pct)的运算结果仍然为NULL。

flow control function

  • IF(condition,value1,value2), if conditionit is true, return it value1, otherwise return it value2.
  • IFNULL(value1,value2), if value1not NULL, return value1otherwise value2.
SELECT employee_id,salary "月工资",commission_pct,12*salary*(1+IF(commission_pct IS NOT NULL,commission_pct,0)) "年收入"
FROM employees;

SELECT employee_id,salary "月工资",commission_pct,12*salary*(1+IFNULL(commission_pct,0)) "年收入"
FROM employees;

LIMIT syntax

There are two types of LIMIT syntax,

  • LIMIT 偏移量,条目数, for example, SELECT ... FROM ... LIMIT 44,2, offset 44, 2 records.
  • LIMIT 条目数 OFFSET 偏移量. For example, SELECT ... FROM ... LIMIT 2 OFFSET 44, offset 44, 2 records.
SELECT employee_id,salary "月工资",commission_pct,12*salary*(1+IF(commission_pct IS NOT NULL,commission_pct,0)) "年收入"
FROM employees 
LIMIT 2 OFFSET 44;

SELECT employee_id,salary "月工资",commission_pct,salary*(1+IFNULL(commission_pct,0)) "年收入"
FROM employees 
LIMIT 2 OFFSET 44;

SELECT employee_id,salary "月工资",commission_pct,salary*(1+IF(commission_pct IS NOT NULL,commission_pct,0)) "年收入"
FROM employees 
LIMIT 44,2;

/*
返回2行记录
employee_id  月工资     commission_pct 年收入
144	         2500.00    (Null)       30000.00
145	         14000.00	0.40	       235200.00
*/

Emphasis (``)

When naming databases, tables, and fields, you must ensure that the database name, table name, and field name do not conflict with reserved words, database systems, or common methods. In case of a conflict, enclose the SQL statement with a parenthesis ( `` ).

# 错误示范
SELECT *
FROM order;
/*
报错如下,因为表名order 与 MySQL关键字(ORDER BY)冲突了
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order'
*/

# 正确示范
# 使用着重号``
SELECT *
FROM `order`;

query constant

SELECT '快乐联萌',employee_id,department_id,last_name
FROM employees
LIMIT 3;

/*
返回3行记录,如下:
快乐联萌    employee_id department_id last_name
快乐联萌	100	        90	          King
快乐联萌	101	        90	          Kochhar
快乐联萌	102	        90	          De Haan
*/

show table structure

Display the table structure, the syntax is as follows:

  • DESC 表名. for example,DESC employees;
  • DESCRIBE 表名. for example,DESCRIBE employees;

Guess you like

Origin blog.csdn.net/qzw752890913/article/details/125993435