SQL第二天

Posted lsls

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL第二天相关的知识,希望对你有一定的参考价值。

筛选条件

技术分享图片

筛选:

功能:
mysql> SELECT * FROM students WHERE age>18;
数据库 选择 所有 从 表名称 其中 年龄大于18的 = 选择表中年龄大于18的
运行结果如下:
+-----------+----------+------+------------+ | number | name | age | birth | +-----------+----------+------+------------+ | 201804004 | Gollum4 | 19 | 2001-01-04 | | 201804005 | Gollum5 | 20 | 2000-01-05 | | 201804006 | Gollum6 | 21 | 1999-01-06 | | 201804007 | Gollum7 | 22 | 1999-01-07 | | 201804008 | Gollum8 | 23 | 1999-01-08 | | 201804009 | Gollum9 | 24 | 1999-01-09 | | 201804010 | Gollum10 | 25 | 1999-01-10 | +-----------+----------+------+------------+ 7 rows in set (0.00 sec)
mysql> SELECT * FROM students WHERE age<>18;
不等于18的数据

+-----------+----------+------+------------+
| number | name | age | birth |
+-----------+----------+------+------------+
| 201804001 | Gollum1 | 16 | 2002-01-01 |
| 201804002 | Gollum2 | 17 | 2001-01-02 |
| 201804004 | Gollum4 | 19 | 2001-01-04 |
| 201804005 | Gollum5 | 20 | 2000-01-05 |
| 201804006 | Gollum6 | 21 | 1999-01-06 |
| 201804007 | Gollum7 | 22 | 1999-01-07 |
| 201804008 | Gollum8 | 23 | 1999-01-08 |
| 201804009 | Gollum9 | 24 | 1999-01-09 |
| 201804010 | Gollum10 | 25 | 1999-01-10 |
| 201804012 | Gollum12 | 12 | 1999-01-10 |
+-----------+----------+------+------------+

重点:

age BETWEEN 12 AND 18; 查询12-18之间的数据
name = 666 OR name = 777;  输出参数等于666或777的数据
age in(12,18); 输出 12和 18

排序(order by)

SELECT * FROM students ORDER BY age;: 正序从小到大排列

SELECT * FROM students ORDER BY age desc;逆序从大到小

限制 linmit

SELECT * FROM students LIMIT 3; 输出前三的数据
SELECT * FROM students LIMIT 3,4; 不取前三条返回前三的后四条

去重 distinct

SELECT DISTINCT grade FROM grades; 查询科目有几种 类似筛选
SELECT * FROM students WHERE name LIKEGollum%;
匹配大概是Gollum的数据%是任意值 
SELECT * FROM students WHERE name LIKEGollum_;
_是任意一个字符

常用的聚合函数

SELECT COUNT(*)FROM students;
查询这个表的个数
SELECT MAX(age)FROM students;查询这个字段的最大值

 


















以上是关于SQL第二天的主要内容,如果未能解决你的问题,请参考以下文章

重闯Sqli-labs关卡第二天(5关)

sql第二天

oracle第二天

SQL第二天

开工第二天

SQL server如何获取给定月的倒数数第二天