SQL学习指南

  1. 简单的select语句

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

使用 NOT 过滤不满足条件的数据

使用 AND 连接多条件

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'

使用 OR 连接多个条件

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

使用 IS NULL 查询空数据

Query for course information about the number of students within the specified range

使用 NOT IN 排除

使用 BETWEEN AND 查询两值间的数据范围

使用 LIKE 模糊查询

使用 IN 查询多条件

4.ORDER BY 与 LIMIT

Check the age of teachers and sort them in ascending order

Sorted by age of Chinese teachers in descending order

使用 ORDER BY 对数据进行排序

Search for the oldest Chinese teacher

使用 LIMIT 限制输出行数

阶段二:函数

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

使用 MIN() 函数返回指定列中的最小值

使用 SUM() 函数统计数值列的总数

使用 AVG() 函数求数值列的平均值

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

使用 ROUND() 函数将数值四舍五入

使用 NULL() 函数判断空值

使用 COUNT() 函数计数

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

使用 EXTRACT() 函数提取指定的时间信息

使用 DATE()、TIME() 函数提取日期和时间

使用 DATE_FORMAT() 格式化输出日期

4.时间函数(二)

Calculate the number of months difference between the start date and the current date of all courses in the schedule

Postpone all course creation dates by one day

使用 DATE_SUB() 减少时间

使用 DATE_ADD() 增加时间

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.约束

检查约束 CHEC

主键约束 PRIMARY KEY

非空约束 NOT NULL

默认约束 DEFAULT

唯一约束 UNIQUE

外键约束 FOREIGN KEY

2.多表连结

联结

内连接 INNER JOIN

交叉连接 CROSS JOIN

外连接 OUTER JOIN

阶段五:查询

1.分组查询

HAVING 子句

GROUP BY 子句

2.简单的子查询

INSERT 语句中的子查询

UPDATE 语句中的子查询

SELECT 语句中的子查询

DELETE 语句中的子查询

3.子查询进阶

多列子查询

ALL 操作符的多行子查询

IN 操作符的多行子查询

内联视图子查询

HAVING 子句中的子查询

ANY 操作符的多行子查询

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值