其他分享
首页 > 其他分享> > 03 基本的Select语句

03 基本的Select语句

作者:互联网

3.1 select .....

select 1; # 没有字句
/*
查询结果:
+---+
| 1 |
+---+
| 1 |
+---+
1 row in set (0.01 sec)
*/

3.2 select ... from ...

# 语法
select 标识选择哪些列(*表示全选) from 表示从那个表中选择;

# 例子
select * from departments;
/*
+---------------+----------------------+------------+-------------+
| department_id | department_name      | manager_id | location_id |
+---------------+----------------------+------------+-------------+
|            10 | Administration       |        200 |        1700 |
|            20 | Marketing            |        201 |        1800 |
|            30 | Purchasing           |        114 |        1700 |
|            40 | Human Resources      |        203 |        2400 |
|            50 | Shipping             |        121 |        1500 |
|            60 | IT                   |        103 |        1400 |
|            70 | Public Relations     |        204 |        2700 |
|            80 | Sales                |        145 |        2500 |
|            90 | Executive            |        100 |        1700 |
|           100 | Finance              |        108 |        1700 |
|           110 | Accounting           |        205 |        1700 |
|           120 | Treasury             | NULL       |        1700 |
|           130 | Corporate Tax        | NULL       |        1700 |
|           140 | Control And Credit   | NULL       |        1700 |
|           150 | Shareholder Services | NULL       |        1700 |
|           160 | Benefits             | NULL       |        1700 |
|           170 | Manufacturing        | NULL       |        1700 |
|           180 | Construction         | NULL       |        1700 |
|           190 | Contracting          | NULL       |        1700 |
|           200 | Operations           | NULL       |        1700 |
|           210 | IT Support           | NULL       |        1700 |
|           220 | NOC                  | NULL       |        1700 |
|           230 | IT Helpdesk          | NULL       |        1700 |
|           240 | Government Sales     | NULL       |        1700 |
|           250 | Retail Sales         | NULL       |        1700 |
|           260 | Recruiting           | NULL       |        1700 |
|           270 | Payroll              | NULL       |        1700 |
+---------------+----------------------+------------+-------------+
27 rows in set (0.05 sec)
*/

# 选择特定的列
select manager_id, location_id from departments;
/*
+------------+-------------+
| manager_id | location_id |
+------------+-------------+
|        200 |        1700 |
|        201 |        1800 |
|        114 |        1700 |
|        203 |        2400 |
|        121 |        1500 |
|        103 |        1400 |
|        204 |        2700 |
|        145 |        2500 |
|        100 |        1700 |
|        108 |        1700 |
|        205 |        1700 |
| NULL       |        1700 |
| NULL       |        1700 |
| NULL       |        1700 |
| NULL       |        1700 |
| NULL       |        1700 |
| NULL       |        1700 |
| NULL       |        1700 |
| NULL       |        1700 |
| NULL       |        1700 |
| NULL       |        1700 |
| NULL       |        1700 |
| NULL       |        1700 |
| NULL       |        1700 |
| NULL       |        1700 |
| NULL       |        1700 |
| NULL       |        1700 |
+------------+-------------+
*/

3.3 列的别名

select last_name as lname, commission_pct as comm from employees;
# 省略as也可以
select last_name lname, commission_pct comm from employees;
/*
+-------------+------+
| lname       | comm |
+-------------+------+
| King        | NULL |
| Kochhar     | NULL |
| De Haan     | NULL |
| Hunold      | NULL |
| Ernst       | NULL |
| Austin      | NULL |
| Pataballa   | NULL |
| Lorentz     | NULL |
| Greenberg   | NULL |
*/

3.4 去除重复行

通过DISTINCT关键词来去除重复行

SELECT DISTINCT department_id FROM employees;
/*
+---------------+
| department_id |
+---------------+
| NULL          |
|            10 |
|            20 |
|            30 |
|            40 |
|            50 |
|            60 |
|            70 |
|            80 |
|            90 |
|           100 |
|           110 |
+---------------+
12 rows in set (0.05 sec)
*/

# 这条语句会查询出所有列,因为employee_id是表的主键,唯一的,因此每一列都不是重复列
SELECT DISTINCT department_id, employee_id FROM employees;

3.5 空值参与运算

# 查询员工一年的工资
SELECT employee_id,salary,commission_pct,
12 * salary * (1 + commission_pct) "annual_sal"
FROM employees;

注:null不等于空字符串。一个空字符串的长度是0,空值的长度是空。

3.6 着重号

当表名或者是列名和关键词或者已有函数重名需要用着重号

select * from ORDER; 
/*1064 - 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
*/

select * from `ORDER`;
/*
+----------+------------+
| order_id | order_name |
+----------+------------+
|        1 | shkstart   |
|        2 | tomcat     |
|        3 | dubbo      |
+----------+------------+
3 rows in set (0.04 sec)
*/

3.7 查询常数

查询的时候添加长度可以增加一列字段,用于整合不同的数据源

select 'jiang' as corporation, last_name as lname from employees;
/*
+-------------+-------------+
| corporation | lname       |
+-------------+-------------+
| jiang       | King        |
| jiang       | Kochhar     |
| jiang       | De Haan     |
| jiang       | Hunold      |
| jiang       | Ernst       |
| jiang       | Austin      |
| jiang       | Pataballa   |
| jiang       | Lorentz     |
| jiang       | Greenberg   |
| jiang       | Faviet      |
| jiang       | Chen        |
*/

3.8 显示表结构

使用DESCRIBE或者DESC命令,表示表结构。

DESCRIBE employees;
DESC employees;
/*
+-----------------+-------------+------+-----+---------+-------+
| Field           | Type        | Null | Key | Default | Extra |
+-----------------+-------------+------+-----+---------+-------+
| department_id   | int         | NO   | PRI | 0       |       |
| department_name | varchar(30) | NO   |     | NULL    |       |
| manager_id      | int         | YES  | MUL | NULL    |       |
| location_id     | int         | YES  | MUL | NULL    |       |
+-----------------+-------------+------+-----+---------+-------+
4 rows in set (0.05 sec)
*/

3.10 过滤表数据

用where条件来过滤数据

SELECT 字段1,字段2
FROM 表名
WHERE 过滤条件;
# WHERE需要紧跟这FROM后面

# 查询部门id为90的员工号、姓名、工作id、部门id
SELECT employee_id, last_name, job_id, department_id
FROM employees
WHERE department_id = 90 ;

3.10 练习

标签:语句,03,jiang,1700,employees,select,NULL,id,Select
来源: https://www.cnblogs.com/jiangblog/p/16395370.html