Basic SELECT statement and display table structure

Basic SELECT statement

SELECT…

SELECT 1+1, 2+2;# 直接这样写相当于下面这句
SELECT 1+1, 2+2 FROM DUAL; # 这里DUAL:伪表

SELECT … FROM

grammar:

SELECT 标识选择哪些字段()
FROM 标识从哪个表中选择

For example to select all columns:

SELECT *
FROM departments;

In general, unless you need to use all the field data in the table, it is best not to use the wildcard '*'. Using wildcards saves time entering query statements, but fetching data for columns you don't need often slows down the query and the applications you use. The advantage of wildcards is that they can be used to get the names of the columns you want when you don't know them.

In a production environment, it is not recommended to directly use SELECT * to query .

Select specific columns:

SELECT department_id, location_id
FROM departments;

SQL statements in MySQL are case-insensitive, so SELECT and select have the same function, but it is customary to capitalize keywords and lowercase data columns and table names .

column alias

Renames a column (alias alias) for easier calculations. Note that the columns in the result set after renaming will display the alias instead of the original name.

Immediately following the column name, you can also add the keyword AS between the column name and the alias. The alias uses double quotes so that the alias contains spaces or special characters and is case-sensitive. It is recommended that the alias be short and the AS can be omitted.

example

SELECT last_name "Name", salary*12 "Annual Salary"
FROM employees;

remove duplicate rows

By default, the query returns all rows, including duplicates.

SELECT department_id
FROM employees;

Use the keyword DISTINCT in the SELECT statement to remove duplicate rows

SELECT DISTINCT department_id
FROM employees;

Targeted at:

SELECT DISTINCT department_id,salary
FROM employees;

Notice:

  • DISTINCTSELECT salary, DISTINCT department_id
    FROM employeesIt needs to be placed in front of all column names, and an error will be reported if it is written .
  • DISTINCTIn fact, it is to deduplicate the combination of all the following column names. If you want to see what different departments (department_id) there are, you only need to write DISTINCT department_id, and you don’t need to add other column names later.

Null values ​​participate in operations

All operators or column values ​​encounter a null value, and the result of the operation is null. Of course it can be adopted IFNULLas its solution.

SELECT employee_id,salary,commission_pct,
12 * salary * (1 + commission_pct) "annual_sal"
FROM employees;

In MySQL, a null value is not equal to an empty string. An empty string has length 0, and a null value has length NULL. Moreover, in MySQL, null values ​​take up space .

Emphasis

We need to ensure that the fields and table names in the table do not conflict with reserved words, database systems or common methods. If they are really the same, please use a pair of `` (emphasis marks) in the SQL statement.

# 错误
mysql> SELECT * FROM ORDER;
ERROR 1064 (42000): 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' at
line 1

# 正确
mysql> SELECT * FROM `ORDER`;

Query constants (add constant fields to queries at the same time)

SELECT queries can also query constants. Yes, it is to add a fixed constant column to the SELECT query result. The value of this column is specified by us, rather than dynamically fetched from the data table.

For example, we want to query the names of employees in the employees data table, and add a field at the same time corporation. The fixed value of this field is "timerring", which can be written as follows:

SELECT 'timering' as corporation, last_name
FROM employees;

show table structure

Use the DESCRIBE or DESC command to indicate the table structure.

DESCRIBE employees;DESC employees;
mysql> desc employees;
+----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+-------+
| employee_id | int(6) | NO | PRI | 0 | |
| first_name | varchar(20) | YES | | NULL | |
| last_name | varchar(25) | NO | | NULL | |
| email | varchar(25) | NO | UNI | NULL | |
| phone_number | varchar(20) | YES | | NULL | |
| hire_date | date | NO | | NULL | |
| job_id | varchar(10) | NO | MUL | NULL | |
| salary | double(8,2) | YES | | NULL | |
| commission_pct | double(2,2) | YES | | NULL | |
| manager_id | int(6) | YES | MUL | NULL | |
| department_id | int(4) | YES | MUL | NULL | |
+----------------+-------------+------+-----+---------+-------+
11 rows in set (0.00 sec)

Among them, the meaning of each field is explained as follows:

  • Field: Indicates the field name.
  • Type: Indicates the field type, here barcode, it goodsnameis text type, and it priceis integer type.
  • Null: Indicates whether the column can store NULL values.
  • Key: Indicates whether the column is indexed. PRIIndicates that the column is part of the primary key of the table; UNIindicates that the column is part of the UNIQUE index; MULindicates that a given value in the column is allowed to appear multiple times.
  • Default: Indicates whether the column has a default value, and if so, what is the value.
  • Extra: Indicates additional information related to a given column that can be obtained, such as AUTO_INCREMENT, etc.

filter data

SELECT 字段1,字段2
FROM 表名
WHERE 过滤条件
  • Use the WHERE clause to filter out rows that do not meet the conditions
  • The WHERE clause follows the FROM clause

example

SELECT employee_id, last_name, job_id, department_id
FROM employees
WHERE department_id = 90 ;

practice questions

1. Query the total salary of employees for 12 months, and alias it as ANNUAL SALARY

Basic wage

SELECT employee_id, last_name, salary * 12 "ANNUAL SALARY"
FROM employees;

weighted salary

SELECT employee_id, last_name, salary * 12 * ( 1 + IFNULL(commission_pct,0)) "ANNUAL SALARY"
FROM employees;

2. Query the data after removing the duplicate job_id in the employees table

SELECT DISTINCT job_id
FROM employees;

3. Query the name and salary of employees whose salary is greater than 12000

SELECT first_name, last_name, salary
FROM employees
WHERE salary > 12000;

4. Query the name and department number of the employee whose employee number is 176

SELECT first_name, last_name, department_id
FROM employees
WHERE employee_id = 176;

5. Display the structure of the table departments and query all the data in it

DESC departments;
SELECT * FROM departments;

Guess you like

Origin blog.csdn.net/m0_52316372/article/details/128724026