- 理解SQL 语句的执行过程
- 掌握SQL 语句的基本语法
- 掌握SQL 语句的增删改查操作
1.SQL 分类
MySQL 是关系型数据库系统,其中存储了大量的数据,通过SQL 管理数据库配置和数据。
结构化查询语言(SQL),对数据库进行操作的语句。
2.DDL 语句
数据定义语句(Data Definition Language,DDL),通过这类语言可以对数据库、表、列等元数据进行创建、删除、更改。
- 数据库的创建、删除、属性修改;
- 表的创建、删除、更新;
- 列的更新,插入等操作。
库表列叫做元数据
3.DML 语句
数据操纵语句(Data Manipulation Language,DML),用于添加(增)、删除(删)、更新(改)和查询(查)数据库记录并检查数据完整性。
4.DCL 语句
数据控制语句(Data Control Language,DCL),通过此类语句可以对数据库的相关权限进行设置。
- 某一个用户是否可以从某一个地址登录数据库;
- 某一个用户是否可以增删改查某一个数据库的表中数据。
5.DDL 语句
数据定义语句。对数据库内部数据库、表、列进行创建、删除、修改等操作的语言,DDL 语句更多的是由数据库管理员DBA
使用。
命令 | 解释 | 示例 |
show databases; | 查看所有数据库列表 | |
use ; | 进入数据库 | use information_schema; |
show tables; | 查看数据库中表名 | |
desc |查看表的定义 | desc mysql.user; |
bbs ----+|+-- user # 存储用户信息|`-- message # 存储用户留言
1.数据库操作
1.创建一个数据库,名为bbs。
MariaDB [(none)]> create database bbs;
Query OK, 1 row affected (0.00 sec)MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| bbs |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)MariaDB [(none)]>
2.删除数据库
删库是一个非常危险的操作,请慎重!
MariaDB [(none)]> drop database bbs;
Query OK, 0 rows affected (0.01 sec)MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)MariaDB [(none)]>
3.创建数据库的同时指定默认编码。
MariaDB [(none)]> create database bbs default character set utf8;
Query OK, 1 row affected (0.00 sec)MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| bbs |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)MariaDB [(none)]>
4.切入数据库bbs。
MariaDB [(none)]> use bbs;
Database changed
MariaDB [bbs]>
2.表操作
1.进行表的操作之前,需要确定表所在数据库。
MariaDB [bbs]> select database();
+------------+
| database() |
+------------+
| bbs |
+------------+
1 row in set (0.00 sec)MariaDB [bbs]>
2.数据库规划,user 表:
user # 存储用户信息的id # 用户id,主键,int(10)username # 用户名,varchar(255)password # 用户密码 varchar(255)imgpath # 用户的头像 varchar(255)
3.创建user 表(create table user)
MariaDB [bbs]> create table user(id int(10) not null auto_increment primary key, username varchar(255) not null, password varchar(255) not null, imgpath varchar(255));
Query OK, 0 rows affected (0.00 sec)MariaDB [bbs]> show tables;
+---------------+
| Tables_in_bbs |
+---------------+
| user |
+---------------+
1 row in set (0.00 sec)MariaDB [bbs]> desc user;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| username | varchar(255) | NO | | NULL | |
| password | varchar(255) | NO | | NULL | |
| imgpath | varchar(255) | YES | | NULL | |
+----------+--------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)MariaDB [bbs]> show create table user\G
*************************** 1. row ***************************Table: user
Create Table: CREATE TABLE `user` (`id` int(10) NOT NULL AUTO_INCREMENT,`username` varchar(255) NOT NULL,`password` varchar(255) NOT NULL,`imgpath` varchar(255) DEFAULT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)MariaDB [bbs]>
注意:
- \G 就是用来代替; 使用,以显示更多的内容。
4.数据库规划,message 表:
message # 存储用户留言信息id # 留言的 ID,主键,int(10)uid # 该条留言的主人的ID int(10)title # 留言的标题 varchar(255)content # 留言的内容 varchar(255)
5.创建message 表
MariaDB [bbs]> create table message (id int(10) not null auto_increment primary key, uid int(10) not null, title varchar(255) not null, content varchar(255) not null);
Query OK, 0 rows affected (0.01 sec)MariaDB [bbs]> show create table message\G
*************************** 1. row ***************************Table: message
Create Table: CREATE TABLE `message` (`id` int(10) NOT NULL AUTO_INCREMENT,`uid` int(10) NOT NULL,`title` varchar(255) NOT NULL,`content` varchar(255) NOT NULL,PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)MariaDB [bbs]>
6.表的删除
删除操作要慎重,变通的方法是给表改名。
MariaDB [bbs]> drop table user;
Query OK, 0 rows affected (0.00 sec)MariaDB [bbs]> drop table message;
Query OK, 0 rows affected (0.00 sec)MariaDB [bbs]>
7.更改表名
基本句式为alter table 表名 rename [to] 新的表名;
,将表user 的名字改为users。
MariaDB [bbs]> alter table user rename users;
Query OK, 0 rows affected (0.01 sec)MariaDB [bbs]> show tables;
+---------------+
| Tables_in_bbs |
+---------------+
| message |
| users |
+---------------+
2 rows in set (0.00 sec)MariaDB [bbs]>
3.列操作
1.查看表的定义
MariaDB [bbs]> desc users;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| username | varchar(255) | NO | | NULL | |
| password | varchar(255) | NO | | NULL | |
| imgpath | varchar(255) | YES | | NULL | |
+----------+--------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)MariaDB [bbs]>
2.修改表中列类型
基本句式为alter table 表名 modify [column] 列名 列定义 [first|after 列名];
。将表user 中字段username 的类型修改为varchar(50),并将其移动到pasword 字段后面。
MariaDB [bbs]> alter table users modify username varchar(50) not null after password;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0MariaDB [bbs]> desc users;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| password | varchar(255) | NO | | NULL | |
| username | varchar(50) | NO | | NULL | |
| imgpath | varchar(255) | YES | | NULL | |
+----------+--------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)MariaDB [bbs]>字段
3.删除表列
基本句式为alter table 表名 drop [column] 列名;
,将表user 中字段imgpath 删除。
MariaDB [bbs]> alter table users drop imgpath;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0MariaDB [bbs]> desc users;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| password | varchar(255) | NO | | NULL | |
| username | varchar(50) | NO | | NULL | |
+----------+--------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)MariaDB [bbs]>
4.增加表中列
基本句式为alter table 表名 add [column] 列名 列定义 [first|after 列名];
,向表user 中增加字段imgpath。
MariaDB [bbs]> alter table users add imgpath varchar(255);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0MariaDB [bbs]> desc users;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| password | varchar(255) | NO | | NULL | |
| username | varchar(50) | NO | | NULL | |
| imgpath | varchar(255) | YES | | NULL | |
+----------+--------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)MariaDB [bbs]>
5.列改名
基本句式为alter table 表名 change 旧的列名 新的列名 列定义 [first|after 列名];
,将表user 中字段imgpath 的名字改为img_path。
MariaDB [bbs]> alter table users change imgpath img_path varchar(255);
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0MariaDB [bbs]> desc users;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(10) | NO | PRI | NULL | auto_increment |
| password | varchar(255) | NO | | NULL | |
| username | varchar(50) | NO | | NULL | |
| img_path | varchar(255) | YES | | NULL | |
+----------+--------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)MariaDB [bbs]>
6.DML 语句
- insert
- delete
- update
- select
1.查询所有内容
MariaDB [bbs]> select * from users;
Empty set (0.00 sec)MariaDB [bbs]>
2.跨库跨表查询
select * from mysql.users;
select * from mysql.users\G
注意:
- select 的查询结果是一张表,而且是一张虚拟的表;
- select 语句不会修改表中原来的数据。
3.插入记录
- 核心思路:向哪张表中的哪个字段中插入数据,内容是什么?
- 插入记录,基本句式为
insert into 表名( 列名1, 列名2,...) values( '值1', '值2', ...);
,向表users 中插入用户ajest,其密码是123456。
MariaDB [bbs]> insert into users( username, password) values( 'ajest', '123456');
Query OK, 1 row affected (0.01 sec)MariaDB [bbs]> select * from users;
+----+----------+----------+----------+
| id | password | username | img_path |
+----+----------+----------+----------+
| 1 | 123456 | ajest | NULL |
+----+----------+----------+----------+
1 row in set (0.00 sec)MariaDB [bbs]> insert into users( username, password) values( 'root', '123456'), ( 'admin', '123456');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0MariaDB [bbs]> select * from users;
+----+----------+----------+----------+
| id | password | username | img_path |
+----+----------+----------+----------+
| 1 | 123456 | ajest | NULL |
| 2 | 123456 | root | NULL |
| 3 | 123456 | admin | NULL |
+----+----------+----------+----------+
3 rows in set (0.00 sec)MariaDB [bbs]>
批量插入留言
MariaDB [bbs]> insert into message(uid,title,content)values(1,"Hello","My Name is AJEST"),(2,"OK?","Hi,AJEST!How are you?"),(1,"ok!","I'm fine!Thank you!"),(3,"Hello","My Name is admin!");
Query OK, 4 rows affected (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 0MariaDB [bbs]> select * from message;
+----+-----+-------+-----------------------+
| id | uid | title | content |
+----+-----+-------+-----------------------+
| 1 | 1 | Hello | My Name is AJEST |
| 2 | 2 | OK? | Hi,AJEST!How are you? |
| 3 | 1 | ok! | I'm fine!Thank you! |
| 4 | 3 | Hello | My Name is admin! |
+----+-----+-------+-----------------------+
4 rows in set (0.00 sec)MariaDB [bbs]>
思考与练习:
- root 用户进行留言,题目是Hero,内容是I want to be a hero!
insert into messages(uid,title,content)values(2,"Hero","I want to be a hero!");
- 新注册一个用户,名字是tom,密码是[123.com]。
insert into users(username,password)values("tom","123.com");
- tom 留言,内容是Where are you, jerry?
insert into messages(uid,content)values(4,"Where are you,jerry?",);
4.更新记录
核心思路:将哪张表中的哪个列更新(修改)为指定的值,过滤条件是什么?
修改用户ajest 的密码为http://123.com。
MariaDB [bbs]> update users set password="123.com";
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0MariaDB [bbs]> select * from users;
+----+----------+----------+----------+
| id | password | username | img_path |
+----+----------+----------+----------+
| 1 | 123.com | ajest | NULL |
| 2 | 123.com | root | NULL |
| 3 | 123.com | admin | NULL |
+----+----------+----------+----------+
3 rows in set (0.01 sec)MariaDB [bbs]> update users set password="123456";
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0MariaDB [bbs]> select * from users;
+----+----------+----------+----------+
| id | password | username | img_path |
+----+----------+----------+----------+
| 1 | 123456 | ajest | NULL |
| 2 | 123456 | r/oot | NULL |
| 3 | 123456 | admin | NULL |
+----+----------+----------+----------+
3 rows in set (0.00 sec)MariaDB [bbs]> update users set password='123.com' where id=1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0MariaDB [bbs]> select * from users;
+----+----------+----------+----------+
| id | password | username | img_path |
+----+----------+----------+----------+
| 1 | 123.com | ajest | NULL |
| 2 | 123456 | root | NULL |
| 3 | 123456 | admin | NULL |
+----+----------+----------+----------+
3 rows in set (0.00 sec)MariaDB [bbs]>
删除记录
核心思路:从哪张表中删除记录,条件是什么?
从表users 中删除用户ajest。
delete from users where id=1;
查询记录
核心思路:确定查询的内容,从哪张表中查,过滤条件是什么?
查询表users 中所有用户名。
MariaDB [bbs]> select username from users;
+----------+
| username |
+----------+
| ajest |
| root |
| admin |
+----------+
3 rows in set (0.00 sec)MariaDB [bbs]>
查询用户名以及密码。
MariaDB [bbs]>
+----------+----------+
| username | password |
+----------+----------+
| ajest | 123.com |
| root | 123456 |
| admin | 123456 |
+----------+----------+
3 rows in set (0.00 sec)MariaDB [bbs]>
5.查询进阶
1.查询不重复记录
关键字distinct 放在字段前面,起到修饰作用,过滤掉重复记录。
MariaDB [bbs]> select distinct password from users;
+----------+
| password |
+----------+
| 123.com |
| 123456 |
+----------+
2 rows in set (0.01 sec)MariaDB [bbs]>
2.条件查询
where
按照一定的条件,将表的一部分输出到屏幕中。
- 如果屏幕中有内容输出,认为过滤条件为真;
- 如果屏幕中无内容输出,认为过滤条件为假;
过滤条件 | 查询内容 |
真 | 有 |
假 | 无 |
查询用户ajest 的所有信息。
MariaDB [bbs]> select * from users where id=1;
+----+----------+----------+----------+
| id | password | username | img_path |
+----+----------+----------+----------+
| 1 | 123.com | ajest | NULL |
+----+----------+----------+----------+
1 row in set (0.00 sec)MariaDB [bbs]>
查询root 用户的密码。
MariaDB [bbs]> select password from users where username='root';
+----------+
| password |
+----------+
| 123456 |
+----------+
1 row in set (0.00 sec)MariaDB [bbs]>
思考与练习:
- 如何查询用户ajest 的id?
select id from users where username='ajest';
- 用户ajest 所有的留言是什么?
select * from messages where uid='1';
6.排序
关键字order by 对查询结果,依据列内容的规律进行排序。
1.(默认)升序排序,使用关键字asc
。
MariaDB [bbs]> select * from message order by id asc;
+----+-----+-------+-----------------------+
| id | uid | title | content |
+----+-----+-------+-----------------------+
| 1 | 1 | Hello | My Name is AJEST |
| 2 | 2 | OK? | Hi,AJEST!How are you? |
| 3 | 1 | ok! | I'm fine!Thank you! |
| 4 | 3 | Hello | My Name is admin! |
+----+-----+-------+-----------------------+
4 rows in set (0.00 sec)MariaDB [bbs]>
2.降序,使用关键字desc
。
MariaDB [bbs]> select * from message order by id desc;
+----+----------+----------+----------+
| id | password | username | img_path |
+----+----------+----------+----------+
| 2 | 123456 | root | NULL |
| 1 | 123.com | ajest | NULL |
| 3 | 123456 | admin | NULL |
+----+----------+----------+----------+
3 rows in set (0.00 sec)MariaDB [bbs]>
按照message 表中,第二列进行排序。
MariaDB [bbs]> select * from message order by 2;
+----+-----+-------+-----------------------+
| id | uid | title | content |
+----+-----+-------+-----------------------+
| 1 | 1 | Hello | My Name is AJEST |
| 3 | 1 | ok! | I'm fine!Thank you! |
| 2 | 2 | OK? | Hi,AJEST!How are you? |
| 4 | 3 | Hello | My Name is admin! |
+----+-----+-------+-----------------------+
4 rows in set (0.00 sec)MariaDB [bbs]>
7.限制
可以用limit
关键字从结果集合中取得(第几条开始,取得几条)记录。
1.查询第一条留言
MariaDB [bbs]> select * from message where id = 1;
+----+-----+-------+------------------+
| id | uid | title | content |
+----+-----+-------+------------------+
| 1 | 1 | Hello | My Name is AJEST |
+----+-----+-------+------------------+
1 row in set (0.00 sec)MariaDB [bbs]>
2.查询最后一条留言
MariaDB [bbs]> select * from message order by id desc limit 0,1;
+----+-----+-------+-------------------+
| id | uid | title | content |
+----+-----+-------+-------------------+
| 4 | 3 | Hello | My Name is admin! |
+----+-----+-------+-------------------+
1 row in set (0.00 sec)MariaDB [bbs]>
8.聚合
聚合就是对表中记录进行统计。
1.使用sum()
计算message 表中所有id 值之和
MariaDB [bbs]> select id from message;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 4 |
+----+
4 rows in set (0.00 sec)MariaDB [bbs]> select sum(id) from message;
+---------+
| sum(id) |
+---------+
| 10 |
+---------+
1 row in set (0.00 sec)MariaDB [bbs]>
2.使用count()
计算出users 表中用户数量。
MariaDB [bbs]> select count(*) from users;
+----------+
| count(*) |
+----------+
| 4 |
+----------+
1 row in set (0.00 sec)MariaDB [bbs]>
4.使用max()
统计message.id 字段记录中最大值。
MariaDB [bbs]> select id from message;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
| 4 |
+----+
4 rows in set (0.00 sec)MariaDB [bbs]> select max(id) from message;
+---------+
| max(id) |
+---------+
| 4 |
+---------+
1 row in set (0.00 sec)MariaDB [bbs]>
5.使用min()
统计message.id 字段记录中最小值。
MariaDB [bbs]> select min(id) from message;
+---------+
| min(id) |
+---------+
| 1 |
+---------+
1 row in set (0.00 sec)MariaDB [bbs]>
6.GROUP BY 分类聚合
:
通过一个key 进行分组计数。
统计每个用户留言的数量。
MariaDB [bbs]> select uid,count(*) from message group by uid;
+-----+----------+
| uid | count(*) |
+-----+----------+
| 1 | 2 |
| 2 | 1 |
| 3 | 1 |
+-----+----------+
3 rows in set (0.00 sec)MariaDB [bbs]>
9.子查询
一个查询需要另外一个查询的结果参与的时候,用子查询。
查询出所有留过言用户的名字
MariaDB [bbs]> select username from users where id in (select uid from message);
+----------+
| username |
+----------+
| ajest |
| root |
| admin |
+----------+
3 rows in set (0.01 sec)MariaDB [bbs]>
查询出所有没有留过言用户的名字
MariaDB [bbs]> select username from users where id not in (select uid from message);
Empty set (0.00 sec)MariaDB [bbs]>
关键字exists 结果集合中是否有记录
MariaDB [bbs]> select exists(select * from users);
+-----------------------------+
| exists(select * from users) |
+-----------------------------+
| 1 |
+-----------------------------+
1 row in set (0.00 sec)MariaDB [bbs]>
not exists 与exists 相反。
MariaDB [bbs]> select not exists(select * from users where id=0);
+--------------------------------------------+
| not exists(select * from users where id=0) |
+--------------------------------------------+
| 1 |
+--------------------------------------------+
1 row in set (0.00 sec)MariaDB [bbs]>
10.表连接
同时查询多张表。
同时查询出用户名,留言标题,留言内容。
MariaDB [bbs]> select user.username, message.title, message.content from users, message where users.id=message.uid;
+----------+-------+-----------------------+
| username | title | content |
+----------+-------+-----------------------+
| ajest | Hello | My Name is AJEST |
| root | OK? | Hi,AJEST!How are you? |
| ajest | ok! | I'm fine!Thank you! |
| admin | Hello | My Name is admin! |
+----------+-------+-----------------------+
4 rows in set (0.01 sec)MariaDB [bbs]>
除了以上写法,还有很多其他变形语句,SQL 语句可以非常灵活,大家注意识别。
select u.username, m.title, m.content from users as u, message as m where u.id=m.uid;select u.username, m.title, m.content from users u, message m where u.id=m.uid;select u.username, m.title, m.content from (select * from users)u, (select * from message)m where u.id=m.uid;
左连接:横向拼接。
MariaDB [bbs]> select u.username, m.title, m.content from users u left join message m on u.id=m.uid;
+----------+-------+-----------------------+
| username | title | content |
+----------+-------+-----------------------+
| ajest | Hello | My Name is AJEST |
| root | OK? | Hi,AJEST!How are you? |
| ajest | ok! | I'm fine!Thank you! |
| admin | Hello | My Name is admin! |
+----------+-------+-----------------------+
4 rows in set (0.00 sec)MariaDB [bbs]>
11.联合查询
同时查询多张,纵向链接
利用联合查询,查出所用用户和留言信息
MariaDB [bbs]> select * from users union select * from message;
+----+----------+----------+-----------------------+
| id | password | username | img_path |
+----+----------+----------+-----------------------+
| 1 | 123.com | ajest | NULL |
| 2 | 123456 | root | NULL |
| 3 | 123456 | admin | NULL |
| 1 | 1 | Hello | My Name is AJEST |
| 2 | 2 | OK? | Hi,AJEST!How are you? |
| 3 | 1 | ok! | I'm fine!Thank you! |
| 4 | 3 | Hello | My Name is admin! |
+----+----------+----------+-----------------------+
7 rows in set (0.00 sec)MariaDB [bbs]>
两张表具有相同的列数。
MariaDB [bbs]> select 1,2,3,4;
+---+---+---+---+
| 1 | 2 | 3 | 4 |
+---+---+---+---+
| 1 | 2 | 3 | 4 |
+---+---+---+---+
1 row in set (0.00 sec)MariaDB [bbs]> select 5,6,7,8,9;
+---+---+---+---+---+
| 5 | 6 | 7 | 8 | 9 |
+---+---+---+---+---+
| 5 | 6 | 7 | 8 | 9 |
+---+---+---+---+---+
1 row in set (0.00 sec)MariaDB [bbs]> select 1,2,3,4 union select 5,6,7,8,9;
ERROR 1222 (21000): The used SELECT statements have a different number of columns
MariaDB [bbs]>
列类型相同(数字比较特殊)。
常见运算符
MySQL 提供了一些运算符号,可以在SQL 语句中使用,比如需要对SQL 语句中的某个值,或者某个字段做运算操作的时候,就可以使用这些运算符。
比较运算符
运算符 | 含义 |
= | 字段等于固定的值 |
> | |
< | |
>= | |
<= | |
<> | 不等于 |
查询出留言id 大于3 的所有留言。
MariaDB [bbs]> select * from message where id > 3;
+----+-----+-------+-------------------+
| id | uid | title | content |
+----+-----+-------+-------------------+
| 4 | 3 | Hello | My Name is admin! |
+----+-----+-------+-------------------+
1 row in set (0.00 sec)MariaDB [bbs]>
逻辑运算符
参与逻辑运算的操作数是布尔类型的值。
- 真,表示为True 或1。
- 假,表示为False 或0。
MariaDB [bbs]> select true;
+------+
| TRUE |
+------+
| 1 |
+------+
1 row in set (0.00 sec)MariaDB [bbs]> select false;
+-------+
| FALSE |
+-------+
| 0 |
+-------+
1 row in set (0.00 sec)MariaDB [bbs]>
and 与运算:有假则假。
and | True | False |
True | True and True = True | True and False = False |
False | False and True = False | False and False = False |
or 或运算:有真则真。
or | True | False |
True | True | True |
False | True | False |
! 非运算:真假取反。
! | True | False |
-- | False | True |
^ 异或运算:异真同假。
^ | True | False |
True | False | True |
False | True | False |
查询出id 在2 到4 之间的留言。
MariaDB [bbs]> select * from message where id >2 and id < 4;
+----+-----+-------+---------------------+
| id | uid | title | content |
+----+-----+-------+---------------------+
| 3 | 1 | ok! | I'm fine!Thank you! |
+----+-----+-------+---------------------+
1 row in set (0.00 sec)MariaDB [bbs]>
逻辑运算对真假性的影响。
语句 | 说明 |
and 1=1 | 判断条件的真假性取决于and 之前语句真假性 |
and 1=2 | 恒假 |
or 1=1 | 恒真 |
算数运算符
运算符 | 含义 |
+ | |
- | |
* | |
/ |
MariaDB [bbs]> select 10/3=1+1;
+----------+
| 10/3=1+1 |
+----------+
| 0 |
+----------+
1 row in set (0.00 sec)MariaDB [bbs]>
运算符优先级
逻辑运算与and 的优先级高于逻辑运算或or。
MariaDB [bbs]> select 1=1 or 1=2 and 1=2;
+--------------------+
| 1=1 or 1=2 and 1=2 |
+--------------------+
| 1 |
+--------------------+
1 row in set (0.00 sec)MariaDB [bbs]> select 1=1 or (1=2 and 1=2);
+----------------------+
| 1=1 or (1=2 and 1=2) |
+----------------------+
| 1 |
+----------------------+
1 row in set (0.00 sec)MariaDB [bbs]> select (1=1 or 1=2) and 1=2;
+----------------------+
| (1=1 or 1=2) and 1=2 |
+----------------------+
| 0 |
+----------------------+
1 row in set (0.00 sec)MariaDB [bbs]>