当前位置:网站首页>基本的SELECT语句
基本的SELECT语句
2022-07-26 16:46:00 【richest_qi】
SELECT …
SELECT 1; -- 返回1
SELECT 1+1;-- 返回2
SELECT 1*3;-- 返回3
SELECT … FROM
DUAL是伪表。SELECT 字段1,字段2,...字段n FROM 表名。SELECT * FROM 表名。其中*代表 表中的所有字段(或者所有列)。
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 … (去除重复行)
SELECT DISTINCT,去除重复行。SELECT DISTINCT department_id,去除department_id重复的行。SELECT DISTINCT department_id,salary,去除department_id、salary同时重复的行。
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同时重复的记录。 */
列的别名
给列取别名,有以下3种方式,
- 列名 别名,比如
SELECT employee_id emp_id FROM employees; - 列名 AS 别名,比如
SELECT employee_id AS emp_id FROM employees; - 列名 “别名”,比如
SELECT 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;
空值参与运算
空值,即NULL。空值参与运算,结果仍然为空值。
SELECT employee_id,salary "月工资",commission_pct,12*salary*(1+commission_pct) "年收入"
FROM employees;
-- 当commission_pct为NULL时,12*salary*(1+commission_pct)的运算结果仍然为NULL。
流控制函数
IF(condition,value1,value2),如果condition为true,则返回value1,否则返回value2。IFNULL(value1,value2),如果value1不为NULL,则返回value1,否则返回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语法
LIMIT语法,有以下两种,
LIMIT 偏移量,条目数,比如,SELECT ... FROM ... LIMIT 44,2,偏移44,2条记录。LIMIT 条目数 OFFSET 偏移量。比如,SELECT ... FROM ... LIMIT 2 OFFSET 44,偏移44,2条记录。
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 */
着重号(``)
给 数据库、表、字段 命名时,必须保证数据库名、表名、字段名,不和保留字、数据库系统或常用方法冲突。万一发生了冲突,在SQL语句中使用着重号(``)引起来。
# 错误示范
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`;
查询常数
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 */
显示表结构
显示表结构,语法如下:
DESC 表名。比如,DESC employees;DESCRIBE 表名。比如,DESCRIBE employees;
边栏推荐
- Chuan Hejing technology's mainland factory was invaded by a virus, and the whole line was shut down!
- Pytorch中的tensor操作
- Mount NFS storage
- MySQL foundation - basic database operation
- Detailed explanation of tcpdump command
- The user experience center of Analysys Qianfan bank was established to help upgrade the user experience of the banking industry
- Detailed explanation of openwrt's feeds.conf.default
- Win11 how to close a shared folder
- On the evolution of cloud native edge computing framework
- In depth exploration of ribbon load balancing
猜你喜欢

About the difference between BigDecimal conversion string toengineeringstring, toplainstring and toString

Stop using xshell and try this more modern terminal connection tool

注意 公安部发出旅游客运交通安全预警

机器学习-什么是机器学习、监督学习和无监督学习

How emqx 5.0 under the new architecture of mria+rlog realizes 100million mqtt connections

Alibaba cloud Toolkit - project one click deployment tool

Realizing DDD based on ABP -- related concepts of DDD

Why are test / development programmers who are better paid than me? Abandoned by the times

Win11 how to close a shared folder

Can TCP and UDP use the same port?
随机推荐
OA项目之我的会议(会议排座&送审)
How to write plug-ins quickly with elisp
Analysis of the advantages of eolink and JMeter interface testing
Is it safe to open an account online now? Who do you want to open a stock account?
Reuse idea through registry
Mount NFS storage
图解用户登录验证流程,写得太好了!
Pass-19,20
Redis persistence - detailed analysis of RDB source code | nanny level analysis! The most complete network
Pytest(思维导图)
regular expression
Crazy God redis notes 02
Current limiting comparison: how to choose sentinel vs hystrix?
Implementing dropout with mxnet from zero sum
【机器学习】Mean Shift原理及代码
"Green is better than blue". Why is TPC the last white lotus to earn interest with money
Is it safe for Guosen Securities to open an account? How can I find the account manager
MySQL foundation - basic database operation
SQL injection (mind map)
What kind of product is the Jetson nano? (how about the performance of Jetson nano)