【7】SQL 语句基础应用

SQL 语句基础应用

  • where (筛选)
    • [where 子句可使用到运算符](#where 子句可使用到运算符)
      • 查询表中所有的数据
      • [查询表中的数据,必须满足 1=1(相当于恒成立)](#查询表中的数据,必须满足 1=1(相当于恒成立))
      • [查询表中的 分数(score) 大于 80 分的学生](#查询表中的 分数(score) 大于 80 分的学生)
      • [查询表中 名称(name) 是 赵六 的数据](#查询表中 名称(name) 是 赵六 的数据)
      • [查询表中 名称(name) 不等于 哈哈 的数据.](#查询表中 名称(name) 不等于 哈哈 的数据.)
      • [查询表中 分数(score) 小于等于 60分 的学生数据](#查询表中 分数(score) 小于等于 60分 的学生数据)
  • 逻辑运算符
    • 查询
      • [查询满足 id 大于 3 并且 分数 大于等于 80 分的学生信息](#查询满足 id 大于 3 并且 分数 大于等于 80 分的学生信息)
      • [查询满足 id 大于 3 或者 分数大于等于 80 分的学生信息](#查询满足 id 大于 3 或者 分数大于等于 80 分的学生信息)
      • [查询名称(name) 不是 哈哈 的学生信息](#查询名称(name) 不是 哈哈 的学生信息)

where (筛选)

在 MySQL 数据表中可以使用 SQL select 语句来读取数据。如果需要有条件的从表中获取数据,可将 where 子句添加到 select 语句中。

语法如下:

sql 复制代码
SELECT
    [ALL | DISTINCT | DISTINCTROW ]
    [HIGH_PRIORITY]
    [STRAIGHT_JOIN]
    [SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
    [SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
    select_expr [, select_expr] ...
    [into_option]
    [FROM table_references
      [PARTITION partition_list]]
    [WHERE where_condition]
    [GROUP BY {col_name | expr | position}, ... [WITH ROLLUP]]
    [HAVING where_condition]
    [WINDOW window_name AS (window_spec)
        [, window_name AS (window_spec)] ...]
    [ORDER BY {col_name | expr | position}
      [ASC | DESC], ... [WITH ROLLUP]]
    [LIMIT {[offset,] row_count | row_count OFFSET offset}]
    [into_option]
    [FOR {UPDATE | SHARE}
        [OF tbl_name [, tbl_name] ...]
        [NOWAIT | SKIP LOCKED]
      | LOCK IN SHARE MODE]
    [into_option]

into_option: {
    INTO OUTFILE 'file_name'
        [CHARACTER SET charset_name]
        export_options
  | INTO DUMPFILE 'file_name'
  | INTO var_name [, var_name] ...
}

SELECT is used to retrieve rows selected from one or more tables, and
can include UNION operations and subqueries. Beginning with MySQL
8.0.31, INTERSECT and EXCEPT operations are also supported. The UNION,
INTERSECT, and EXCEPT operators are described in more detail later in
this section. See also
https://dev.mysql.com/doc/refman/8.0/en/subqueries.html.

A SELECT statement can start with a WITH clause to define common table
expressions accessible within the SELECT. See
https://dev.mysql.com/doc/refman/8.0/en/with.html.

The most commonly used clauses of SELECT statements are these:

o Each select_expr indicates a column that you want to retrieve. There
  must be at least one select_expr.

o table_references indicates the table or tables from which to retrieve
  rows. Its syntax is described in [HELP JOIN].

o SELECT supports explicit partition selection using the PARTITION
  clause with a list of partitions or subpartitions (or both) following
  the name of the table in a table_reference (see [HELP JOIN]). In this
  case, rows are selected only from the partitions listed, and any
  other partitions of the table are ignored. For more information and
  examples, see
  https://dev.mysql.com/doc/refman/8.0/en/partitioning-selection.html.

o The WHERE clause, if given, indicates the condition or conditions
  that rows must satisfy to be selected. where_condition is an
  expression that evaluates to true for each row to be selected. The
  statement selects all rows if there is no WHERE clause.

  In the WHERE expression, you can use any of the functions and
  operators that MySQL supports, except for aggregate (group)
  functions. See
  https://dev.mysql.com/doc/refman/8.0/en/expressions.html, and
  https://dev.mysql.com/doc/refman/8.0/en/functions.html.

SELECT can also be used to retrieve rows computed without reference to
any table.

URL: https://dev.mysql.com/doc/refman/8.0/en/select.html

where 子句可使用到运算符


初始化表数据

sql 复制代码
[root@localhost][db_test]> create table tb_student(
    -> id int not null auto_increment primary key,
    -> name varchar(20),
    -> score int
    -> );
Query OK, 0 rows affected (0.00 sec)

[root@localhost][db_test]> 
[root@localhost][db_test]> insert into tb_student(name, score) values('张三', 95);
Query OK, 1 row affected (0.00 sec)

[root@localhost][db_test]> insert into tb_student(name, score) values('李四', 90);
Query OK, 1 row affected (0.00 sec)

[root@localhost][db_test]> insert into tb_student(name, score) values('王五', 85);
Query OK, 1 row affected (0.00 sec)

[root@localhost][db_test]> insert into tb_student(name, score) values('赵六', 80);
Query OK, 1 row affected (0.01 sec)

[root@localhost][db_test]> insert into tb_student(name, score) values('哈哈', 50);
Query OK, 1 row affected (0.00 sec)

[root@localhost][db_test]> 

查询表中所有的数据

sql 复制代码
[root@localhost][db_test]> select * from tb_student;
+----+--------+-------+
| id | name   | score |
+----+--------+-------+
|  1 | 张三   |    95 |
|  2 | 李四   |    90 |
|  3 | 王五   |    85 |
|  4 | 赵六   |    80 |
|  5 | 哈哈   |    50 |
+----+--------+-------+
5 rows in set (0.00 sec)

[root@localhost][db_test]> 

查询表中的数据,必须满足 1=1(相当于恒成立)

sql 复制代码
[root@localhost][db_test]> select * from tb_student where 1 = 1;
+----+--------+-------+
| id | name   | score |
+----+--------+-------+
|  1 | 张三   |    95 |
|  2 | 李四   |    90 |
|  3 | 王五   |    85 |
|  4 | 赵六   |    80 |
|  5 | 哈哈   |    50 |
+----+--------+-------+
5 rows in set (0.00 sec)

[root@localhost][db_test]> 

查询表中的 分数(score) 大于 80 分的学生

sql 复制代码
[root@localhost][db_test]> select * from tb_student where score > 80;
+----+--------+-------+
| id | name   | score |
+----+--------+-------+
|  1 | 张三   |    95 |
|  2 | 李四   |    90 |
|  3 | 王五   |    85 |
+----+--------+-------+
3 rows in set (0.00 sec)

[root@localhost][db_test]> 

查询表中 名称(name) 是 赵六 的数据

sql 复制代码
[root@localhost][db_test]> select * from tb_student where name = '赵六';
+----+--------+-------+
| id | name   | score |
+----+--------+-------+
|  4 | 赵六   |    80 |
+----+--------+-------+
1 row in set (0.00 sec)

[root@localhost][db_test]> 

查询表中 名称(name) 不等于 哈哈 的数据.

sql 复制代码
[root@localhost][db_test]> select * from tb_student where name != '哈哈';
+----+--------+-------+
| id | name   | score |
+----+--------+-------+
|  1 | 张三   |    95 |
|  2 | 李四   |    90 |
|  3 | 王五   |    85 |
|  4 | 赵六   |    80 |
+----+--------+-------+
4 rows in set (0.00 sec)

[root@localhost][db_test]> 

查询表中 分数(score) 小于等于 60分 的学生数据

sql 复制代码
[root@localhost][db_test]> select * from tb_student where score <= 60;
+----+--------+-------+
| id | name   | score |
+----+--------+-------+
|  5 | 哈哈   |    50 |
+----+--------+-------+
1 row in set (0.00 sec)

[root@localhost][db_test]> 

逻辑运算符

查询

查询满足 id 大于 3 并且 分数 大于等于 80 分的学生信息

sql 复制代码
[root@localhost][db_test]> select * from tb_student where id > 3 and score >= 80;
+----+--------+-------+
| id | name   | score |
+----+--------+-------+
|  4 | 赵六   |    80 |
+----+--------+-------+
1 row in set (0.00 sec)

[root@localhost][db_test]> 

查询满足 id 大于 3 或者 分数大于等于 80 分的学生信息

sql 复制代码
[root@localhost][db_test]> select * from tb_student where id > 3 or score >= 80;
+----+--------+-------+
| id | name   | score |
+----+--------+-------+
|  1 | 张三   |    95 |
|  2 | 李四   |    90 |
|  3 | 王五   |    85 |
|  4 | 赵六   |    80 |
|  5 | 哈哈   |    50 |
+----+--------+-------+
5 rows in set (0.00 sec)

[root@localhost][db_test]> 

查询名称(name) 不是 哈哈 的学生信息

sql 复制代码
[root@localhost][db_test]> select * from tb_student where not name = '哈哈';
+----+--------+-------+
| id | name   | score |
+----+--------+-------+
|  1 | 张三   |    95 |
|  2 | 李四   |    90 |
|  3 | 王五   |    85 |
|  4 | 赵六   |    80 |
+----+--------+-------+
4 rows in set (0.00 sec)

[root@localhost][db_test]> 

上述查询SQL,等同于 如下:

sql 复制代码
[root@localhost][db_test]> select * from tb_student where name <> '哈哈';
+----+--------+-------+
| id | name   | score |
+----+--------+-------+
|  1 | 张三   |    95 |
|  2 | 李四   |    90 |
|  3 | 王五   |    85 |
|  4 | 赵六   |    80 |
+----+--------+-------+
4 rows in set (0.00 sec)

[root@localhost][db_test]> 
相关推荐
serve the people1 小时前
Prompt Serialization in LangChain
数据库·langchain·prompt
万事大吉CC1 小时前
Win11卸载重装oracle 11g数据库
数据库
星光一影1 小时前
打车/网约车、代驾、顺风车/拼车、货运、租车等多种出行服务的一站式解决方案
mysql·微信小程序·php·uniapp·html5·web app
Jing_jing_X2 小时前
MySQL Server 启动后到底加载了什么,创建表插入数据到底怎么存的存在哪
mysql·adb
数据库那些事儿2 小时前
DMS Airflow:企业级数据工作流编排平台的专业实践
数据库
Java水解2 小时前
初识MYSQL —— 基本查询
后端·mysql
一 乐2 小时前
流浪动物救助|流浪猫狗救助|基于Springboot+vue的流浪猫狗救助平台设计与实现(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·毕设
好记忆不如烂笔头abc2 小时前
Configuration of TCP/IP with SSL and TLS for Database Connections
数据库·网络协议·ssl
安全系统学习2 小时前
自学网络安全学习的误区和陷阱
数据库·学习·安全·web安全·网络安全·安全架构
黄色茶杯3 小时前
AI编程工具TRAE解决日常问题之SQLite数据复制
数据库·sqlite