当前位置:网站首页>Basic select statement
Basic select statement
2022-07-26 17:37:00 【richest_ qi】
List of articles
SELECT …
SELECT 1; -- return 1
SELECT 1+1;-- return 2
SELECT 1*3;-- return 3
SELECT … FROM
DUALIt's a pseudo table .SELECT Field 1, Field 2,... Field n FROM Table name.SELECT * FROM Table name. among*representative All fields in the table ( Or all columns ).
SELECT 1
FROM DUAL;
-- among ,DUAL It's a pseudo table .
DESCRIBE DUAL;
-- Report errors :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;
-- *, representative All fields in the table ( Or all columns )
SELECT DISTINCT … ( Remove duplicate lines )
SELECT DISTINCT, Remove duplicate lines .SELECT DISTINCT department_id, Removedepartment_idRepeated lines .SELECT DISTINCT department_id,salary, Removedepartment_id、salaryLines repeated at the same time .
SELECT department_id
FROM employees;
-- return 107 Bar record
# Remove duplicate lines
SELECT DISTINCT department_id
FROM employees;
-- return 12 Bar record
# The negative case
SELECT DISTINCT department_id
FROM employees;
-- return 12 rows
SELECT salary,department_id
FROM employees;
-- return 106 rows
SELECT salary,DISTINCT department_id
FROM employees;
-- Report errors
/* 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 */
# The negative case
SELECT department_id,salary
FROM employees;
-- return 107 rows
SELECT DISTINCT department_id,salary
FROM employees;
-- return 74 rows
SELECT department_id,salary
FROM employees
WHERE department_id = 90
AND salary = 17000;
/* return 2 rows , as follows : department_id salary 90 17000.00 90 17000.00 */
SELECT DISTINCT department_id,salary
FROM employees
WHERE department_id = 90
AND salary = 17000;
/* return 1 rows , as follows , department_id salary 90 17000.00 namely SELECT DISTINCT department_id,salary Removed department_id、salary Duplicate records at the same time . */
Alias of column
Alias the column , There are the following 3 Ways of planting ,
- Name Alias , such as
SELECT employee_id emp_id FROM employees; - Name AS Alias , such as
SELECT employee_id AS emp_id FROM employees; - Name “ Alias ”, such as
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 values participate in the operation
Null value , namely NULL. Null values participate in the operation , The result is still null .
SELECT employee_id,salary " Monthly wages ",commission_pct,12*salary*(1+commission_pct) " Annual income "
FROM employees;
-- When commission_pct by NULL when ,12*salary*(1+commission_pct) The result of the operation is still NULL.
Flow control function
IF(condition,value1,value2), Ifconditionbytrue, Then return tovalue1, Otherwise return tovalue2.IFNULL(value1,value2), Ifvalue1Not forNULL, Then return tovalue1, Otherwise return tovalue2.
SELECT employee_id,salary " Monthly wages ",commission_pct,12*salary*(1+IF(commission_pct IS NOT NULL,commission_pct,0)) " Annual income "
FROM employees;
SELECT employee_id,salary " Monthly wages ",commission_pct,12*salary*(1+IFNULL(commission_pct,0)) " Annual income "
FROM employees;
LIMIT grammar
LIMIT grammar , There are two ,
LIMIT Offset , Number of entries, such as ,SELECT ... FROM ... LIMIT 44,2, The offset 44,2 Bar record .LIMIT Number of entries OFFSET Offset. such as ,SELECT ... FROM ... LIMIT 2 OFFSET 44, The offset 44,2 Bar record .
SELECT employee_id,salary " Monthly wages ",commission_pct,12*salary*(1+IF(commission_pct IS NOT NULL,commission_pct,0)) " Annual income "
FROM employees
LIMIT 2 OFFSET 44;
SELECT employee_id,salary " Monthly wages ",commission_pct,salary*(1+IFNULL(commission_pct,0)) " Annual income "
FROM employees
LIMIT 2 OFFSET 44;
SELECT employee_id,salary " Monthly wages ",commission_pct,salary*(1+IF(commission_pct IS NOT NULL,commission_pct,0)) " Annual income "
FROM employees
LIMIT 44,2;
/* return 2 rows employee_id Monthly wages commission_pct Annual income 144 2500.00 (Null) 30000.00 145 14000.00 0.40 235200.00 */
mark of emphasis (``)
to database 、 surface 、 Field When naming , The database name must be guaranteed 、 Table name 、 Field name , Do not reserve words 、 Database systems or common methods conflict . In case of conflict , stay SQL Use emphasis marks in statements (``) Lead up .
# Error model
SELECT *
FROM order;
/* An error is as follows , Because the table name order And MySQL keyword (ORDER BY) The conflict 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' */
# Correct demonstration
# Use emphasis ``
SELECT *
FROM `order`;
Query constant
SELECT ' Happy Lianmeng ',employee_id,department_id,last_name
FROM employees
LIMIT 3;
/* return 3 rows , as follows : Happy Lianmeng employee_id department_id last_name Happy Lianmeng 100 90 King Happy Lianmeng 101 90 Kochhar Happy Lianmeng 102 90 De Haan */
Display table structure
Display table structure , The grammar is as follows :
DESC Table name. such as ,DESC employees;DESCRIBE Table name. such as ,DESCRIBE employees;
边栏推荐
- 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?
- [machine learning] principle and code of mean shift
- 2022 年有哪些流行的技术?
- Kudu design tablet
- [Luogu p8063] shortest paths (graph theory)
- 我们被一个 kong 的性能 bug 折腾了一个通宵
- API for sellers -- description of the return value of adding baby API to Taobao / tmall sellers' stores
- Week 16 OJ practice 1 calculates the day of the year
- Ascend目标检测与识别-定制自己的AI应用
猜你喜欢

Are CRM and ERP the same thing? What's the difference?

图解用户登录验证流程,写得太好了!

树形dp问题

What kind of product is the Jetson nano? (how about the performance of Jetson nano)
![[ctfshow web] deserialization](/img/cd/b76e148adfc4d61049ab2cf429d4d7.png)
[ctfshow web] deserialization
![[virtual machine data recovery] data recovery cases in which XenServer virtual machine is unavailable due to accidental power failure and virtual disk files are lost](/img/99/e5404a09ec7f52a7c5d7be23e43e85.jpg)
[virtual machine data recovery] data recovery cases in which XenServer virtual machine is unavailable due to accidental power failure and virtual disk files are lost

(25) top level menu of blender source code analysis blender menu

(25)Blender源码分析之顶层菜单Blender菜单

Heavy announcement! Icml2022 Awards: 15 outstanding papers, selected by Fudan University, Xiamen University and Shanghai Jiaotong University

On the evolution of cloud native edge computing framework
随机推荐
Eureka Registry - from entry to application
Use replace regexp to add a sequence number at the beginning of a line
Detailed explanation of openwrt's feeds.conf.default
注意 公安部发出旅游客运交通安全预警
[virtual machine data recovery] data recovery cases in which XenServer virtual machine is unavailable due to accidental power failure and virtual disk files are lost
Realizing DDD based on ABP -- related concepts of DDD
pip安装模块,报错
(24) the top menu of blender source code analysis shows code analysis
After Australia, New Zealand announced the ban on Huawei 5g! Huawei official response
The first self-developed embedded 40nm industrial scale memory chip in China was released, breaking the status quo that the localization rate is zero
ASEMI整流桥KBPC3510,KBPC3510封装,KBPC3510应用
Crazy God redis notes 02
来吧开发者!不只为了 20 万奖金,试试用最好的“积木”来一场头脑风暴吧!...
CCS TM4C123新建工程
How to use different tools to analyze and optimize code performance when CPU utilization is high
Pack tricks
【虚拟机数据恢复】意外断电导致XenServer虚拟机不可用,虚拟磁盘文件丢失的数据恢复案例
Method and voltage setting of exciting vibrating wire sensor with hand-held vibrating wire collector
A collection of commonly used shortcut keys for office software
6-19 vulnerability exploitation -nsf to obtain the target password file