MySQL基本查询
Posted 还小给个面子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL基本查询相关的知识,希望对你有一定的参考价值。
文章目录
表的增删查改
CRUD : Create(创建), Retrieve(读取), Update(更新), Delete(删除)
Create(创建)
基本语法:
INSERT [INTO] table_name
[(column [, column] ...)]
VALUES (value_list) [, (value_list)] ...
value_list: value, [, value] ...
案例:
mysql> create table students (
-> id int unsigned primary key auto_increment,
-> sn int not null unique comment '学号',
-> name varchar(20) not null,
-> email varchar(20)
-> )engine=innodb default charset=utf8;
Query OK, 0 rows affected (0.03 sec)
mysql> desc students;
+-------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| sn | int(11) | NO | UNI | NULL | |
| name | varchar(20) | NO | | NULL | |
| email | varchar(20) | YES | | NULL | |
+-------+------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
单行数据 + 全列插入
插入两条记录,当value_list 数量和定义表的列的数量及顺序一致时,就可以省略value_list。注意,这里在插入的时候,也可以不用指定id,mysql会使用默认的值进行自增。
mysql> insert into students values (100, 1000, 'Curry', NULL);
Query OK, 1 row affected (0.01 sec)
mysql> insert into students values (101, 1001, 'Durant', '3306@163.com');
Query OK, 1 row affected (0.00 sec)
mysql> select * from students;
+-----+------+--------+--------------+
| id | sn | name | email |
+-----+------+--------+--------------+
| 100 | 1000 | Curry | NULL |
| 101 | 1001 | Durant | 3306@163.com |
+-----+------+--------+--------------+
2 rows in set (0.00 sec)
多行数据 + 指定列插入
插入两条记录,value_list 数量必须和指定列数量及顺序一致
mysql> insert into students (id, sn, name) values (102, 1002, 'Kobe'), (103, 1003, 'Klay');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from students;
+-----+------+--------+--------------+
| id | sn | name | email |
+-----+------+--------+--------------+
| 100 | 1000 | Curry | NULL |
| 101 | 1001 | Durant | 3306@163.com |
| 102 | 1002 | Kobe | NULL |
| 103 | 1003 | Klay | NULL |
+-----+------+--------+--------------+
4 rows in set (0.00 sec)
插入否则更新
由于 主键 或者 唯一键 对应的值已经存在而导致插入失败
主键冲突:
mysql> insert into students (id, sn, name) values (100, 1004, 'Brown');
ERROR 1062 (23000): Duplicate entry '100' for key 'PRIMARY'
唯一键冲突:
mysql> insert into students (id, sn, name) values (104, 1003, 'Bryant');
ERROR 1062 (23000): Duplicate entry '1003' for key 'sn'
可以选择性的进行同步更新操作 语法:
INSERT ... ON DUPLICATE KEY UPDATE
column = value [, column = value] ...
mysql> insert into students (id, sn, name) values (104, 1003, 'Bryant')
-> on duplicate key update id=104, name='Bryant';
Query OK, 2 rows affected (0.01 sec)
mysql> select * from students;
+-----+------+--------+--------------+
| id | sn | name | email |
+-----+------+--------+--------------+
| 100 | 1000 | Curry | NULL |
| 101 | 1001 | Durant | 3306@163.com |
| 102 | 1002 | Kobe | NULL |
| 104 | 1003 | Bryant | NULL |
+-----+------+--------+--------------+
4 rows in set (0.00 sec)
- 0 row affected: 表中有冲突数据,但冲突数据的值和 update 的值相等
- 1 row affected: 表中没有冲突数据,数据被插入
- 2 row affected: 表中有冲突数据,并且数据已经被更新
替换
主键 或者 唯一键 没有冲突,则直接插入;
主键 或者 唯一键 如果冲突,则删除后再插入
mysql> replace into students (sn, name) values (1002, 'Mitchell');
Query OK, 2 rows affected (0.00 sec)
mysql> select * from students;
+-----+------+----------+--------------+
| id | sn | name | email |
+-----+------+----------+--------------+
| 100 | 1000 | Curry | NULL |
| 101 | 1001 | Durant | 3306@163.com |
| 104 | 1003 | Bryant | NULL |
| 105 | 1002 | Mitchell | NULL |
+-----+------+----------+--------------+
4 rows in set (0.00 sec)
- 1 row affected: 表中没有冲突数据,数据被插入
- 2 row affected: 表中有冲突数据,删除后重新插入
Retrieve(读取)
基础语法:
SELECT
[DISTINCT] * | column [, column] ...
[FROM table_name]
[WHERE ...]
[ORDER BY column [ASC | DESC], ...]
LIMIT ...
案例:
创建表结构:
mysql> create table exam_result (
-> id int unsigned primary key auto_increment,
-> name varchar(20) not null comment '姓名',
-> chinese float default 0.0 comment '语文成绩',
-> math float default 0.0 comment '数学成绩',
-> english float default 0.0 comment '英语成绩'
-> )engine=innodb default charset=utf8;
Query OK, 0 rows affected (0.02 sec)
插入测试数据:
mysql> insert into exam_result (name, chinese, math, english) values
-> ('唐三藏', 67, 98, 56),
-> ('孙悟空', 87, 78, 77),
-> ('猪悟能', 88, 98, 90),
-> ('曹孟德', 82, 84, 67),
-> ('刘玄德', 55, 85, 45),
-> ('孙权', 70, 73, 78),
-> ('宋公明', 75, 65, 30);
Query OK, 7 rows affected (0.00 sec)
Records: 7 Duplicates: 0 Warnings: 0
SELECT列
全列查询
通常情况下不建议使用 * 进行全列查询
- 查询的列越多,意味着需要传输的数据量越大;
- 可能会影响到索引的使用;
mysql> select * from exam_result;
+----+-----------+---------+------+---------+
| id | name | chinese | math | english |
+----+-----------+---------+------+---------+
| 1 | 唐三藏 | 67 | 98 | 56 |
| 2 | 孙悟空 | 87 | 78 | 77 |
| 3 | 猪悟能 | 88 | 98 | 90 |
| 4 | 曹孟德 | 82 | 84 | 67 |
| 5 | 刘玄德 | 55 | 85 | 45 |
| 6 | 孙权 | 70 | 73 | 78 |
| 7 | 宋公明 | 75 | 65 | 30 |
+----+-----------+---------+------+---------+
7 rows in set (0.00 sec)
指定列查询
指定列的顺序不需要按定义表的顺序来
mysql> select id, name, math from exam_result;
+----+-----------+------+
| id | name | math |
+----+-----------+------+
| 1 | 唐三藏 | 98 |
| 2 | 孙悟空 | 78 |
| 3 | 猪悟能 | 98 |
| 4 | 曹孟德 | 84 |
| 5 | 刘玄德 | 85 |
| 6 | 孙权 | 73 |
| 7 | 宋公明 | 65 |
+----+-----------+------+
7 rows in set (0.00 sec)
查询字段为表达式
表达式不包含字段:
mysql> select id, name, 10 from exam_result;
+----+-----------+----+
| id | name | 10 |
+----+-----------+----+
| 1 | 唐三藏 | 10 |
| 2 | 孙悟空 | 10 |
| 3 | 猪悟能 | 10 |
| 4 | 曹孟德 | 10 |
| 5 | 刘玄德 | 10 |
| 6 | 孙权 | 10 |
| 7 | 宋公明 | 10 |
+----+-----------+----+
7 rows in set (0.00 sec)
表达式包含一个字段:
mysql> select id, name, math+10 from exam_result;
+----+-----------+---------+
| id | name | math+10 |
+----+-----------+---------+
| 1 | 唐三藏 | 108 |
| 2 | 孙悟空 | 88 |
| 3 | 猪悟能 | 108 |
| 4 | 曹孟德 | 94 |
| 5 | 刘玄德 | 95 |
| 6 | 孙权 | 83 |
| 7 | 宋公明 | 75 |
+----+-----------+---------+
7 rows in set (0.00 sec)
表达式包含多个字段:
mysql> select id, name, math+chinese+english from exam_result;
+----+-----------+----------------------+
| id | name | math+chinese+english |
+----+-----------+----------------------+
| 1 | 唐三藏 | 221 |
| 2 | 孙悟空 | 242 |
| 3 | 猪悟能 | 276 |
| 4 | 曹孟德 | 233 |
| 5 | 刘玄德 | 185 |
| 6 | 孙权 | 221 |
| 7 | 宋公明 | 170 |
+----+-----------+----------------------+
7 rows in set (0.00 sec)
查询结果指定别名
基础语法:
ELECT column [AS] alias_name [...] FROM table_name;
mysql> select id, name, math+chinese+english total from exam_result;
+----+-----------+-------+
| id | name | total |
+----+-----------+-------+
| 1 | 唐三藏 | 221 |
| 2 | 孙悟空 | 242 |
| 3 | 猪悟能 | 276 |
| 4 | 曹孟德 | 233 |
| 5 | 刘玄德 | 185 |
| 6 | 孙权 | 221 |
| 7 | 宋公明 | 170 |
+----+-----------+-------+
7 rows in set (0.00 sec)
结果去重
查询结果重复:
mysql> select math from exam_result;
+------+
| math |
+------+
| 98 |
| 78 |
| 98 |
| 84 |
| 85 |
| 73 |
| 65 |
+------+
7 rows in set (0.00 sec)
查询结果去重:
mysql> select distinct math from exam_result;
+------+
| math |
+------+
| 98 |
| 78 |
| 84 |
| 85 |
| 73 |
| 65 |
+------+
6 rows in set (0.00 sec)
WHERE 条件
基本比较
英语不及格的同学及英语成绩 ( < 60 ):
mysql> select name, english from exam_result where english<60;
+-----------+---------+
| name | english |
+-----------+---------+
| 唐三藏 | 56 |
| 刘玄德 | 45 |
| 宋公明 | 30 |
+-----------+---------+
3 rows in set (0.00 sec)
BETWEEN AND 条件连接
语文成绩在 [80, 90] 分的同学及语文成绩:
使用 AND 进行条件连接
mysql> select name, chinese from exam_result where chinese>=80 and chinese<=90;
+-----------+---------+
| name | chinese |
+-----------+---------+
| 孙悟空 | 87 |
| 猪悟能 | 88 |
| 曹孟德 | 82 |
+-----------+---------+
3 rows in set (0.00 sec)
使用 BETWEEN AND 条件连接
以上是关于MySQL基本查询的主要内容,如果未能解决你的问题,请参考以下文章