赞
踩
SELECT
语句是最常用的 SQL 语句,它能帮助我们从一个或多个表中查询信息。查询是数据库中最常用的功能,因此我们选择它作为 SQL 语句学习的第一步。
SELECT 语句用于从数据库中选取数据,并将结果存储在一个临时结果表中,这个表称为结果集。结果集实际上也是一种包含行与列的表,只不过是未持久化的,即临时表。
示例代码
在使用 SELECT 语句检索表数据时,至少需要给出两条信息——想检索的列名(column_name)和被检索内容的表名(table_name)。
大家可能会有些疑惑,列名 column_name 和表名 table_name 左右加的是什么?
这是反引号(``),它就在我们电脑键盘的左上角数字 1 的左边位置。
上述代码不加反引号的效果如下:
可能有些同学会问:在平时编写程序时不常加反引号,那不加反引号可以吗?反引号的作用是什么?
其实是可以的,在绝大部分时候,不加反引号并不会导致程序出错。但其实我们在命名字段的时候,字段名可能会与 SQL 关键字冲突,这时候要用反引号将列名和表名包含一下,避免关键字冲突。因此,在本课程所有小节的学习中,都会更加严谨地加上反引号。
当我们仅需要查询某一列的信息,且知道该列的列名时,可以使用简单的 SELECT COLUMN
的语句查询单个列来获取该列的信息。
1.比较运算符
Query the name of the Chinese teacher
Query teachers over 20 years old
2.逻辑运算符
Query the courses that meet the conditions taught by the specified teacher
Inquire about courses starting before May 2020
Search for courses with an instructor id of less than 3 and more than 800 students
Query the course information of 'Web' or 'Big Data'
3.特殊条件
Query courses with teacher id other than 1 and 3
Query teacher information by email
Inquire about Chinese and Japanese teachers who have e-mail addresses
Query course information for a specific time
Query for course information about the number of students within the specified range
4.ORDER BY 与 LIMIT
Check the age of teachers and sort them in ascending order
Sorted by age of Chinese teachers in descending order
Search for the oldest Chinese teacher
1.算数函数(一)
Check the age of the youngest teacher
Find the age of the oldest Chinese teacher
Count the total number of students for teacher #3
2.算数函数(二)
Check the average age of teachers over 20 years old
Number of teachers aged 20 to 28 who are Chinese and British nationals
Check the information of teachers who do not have email and are older than 20 years old
3.时间函数(一)
Search for course titles and course dates through August 2020
Query the hours of all course creation times
使用 NOW() 、 CURDATE()、CURTIME() 获取当前时间
The date the course was created is displayed in 'year-month-day hour:minute:second'
Insert the current date into the table
4.时间函数(二)
Postpone all course creation dates by one day
Calculate the number of days from 03/26/2019 to the course creation time
Advance all course creation dates by one day
使用时间函数 DATEDIFF() 和 TIMESTAMPDIFF() 计算日期差
1.约束
2.多表连结
阶段五:查询
1.分组查询
2.简单的子查询
3.子查询进阶
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。