MySQL--表的基本查询--0410--15

这篇具有很好参考价值的文章主要介绍了MySQL--表的基本查询--0410--15。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

目录

1. Create

1.1 insert

1.1.2 插入否则更新

1.2 replace

2.Retrieve

2.1 select

2.1.1 全列查询

2.1.2 指定列查询

2.1.3 查询字段为表达式

2.1.4 为查询结果指定名称

 2.1.5 去重

2.2 where

2.2.1  >  and >= and < and <= and =

2.2.2  in  between

2.2.3 查找列数值为null的方法

2.2.4 like 

2.2.5 where中使用表达式

2.3 结果排序

2.3.2 多级排序

2.3.3 显示多个或者有限个

2.3.4 补充

2.4 mysql中select语句的执行顺序

3. update

 3.1 更新数据

 4. delete操作

 4.1 delete

4.1.1 删除某一个数据

 4.1.2 删除整张表的数据

 4.2 truncate

 5.插入查询结果


表的操作,CRUD:create、retrieve、update、delete

1. Create

1.1 insert

语法

INSERT [INTO] table_name
[(column [, column] ...)]
VALUES (value_list) [, (value_list)] ..
  • into 可以不写
  • () values ()   左边的括号代表要对哪些数据进行显式的插入,左括号如果不写则默认对全部列进行插入,右边的括号表示具体的数值。
  • 如果要一次性插入多组数据,可以在一组数据插入完毕后,再跟一组右括号。
mysql> create table t10( id int unsigned primary key auto_increment,
    -> stu_num int not null unique,
    -> name varchar(10),
    -> qq varchar(20)
    -> );

mysql> insert into t10 values (100,1,'steven','12345');

mysql> insert into t10 (stu_num,name.qq) values (3,'kim','1234567');

mysql> insert into t10 (stu_num,name,qq) values (5,'cat','56789'),('6','dog','7890');
mysql> select * from t10;
+-----+---------+--------+---------+
| id  | stu_num | name   | qq      |
+-----+---------+--------+---------+
| 100 |       1 | steven | 12345   |
| 101 |       2 | kim    | 1234567 |
| 102 |       3 | kim    | 1234567 |
| 103 |       4 | ketty  | 5678    |
| 104 |       5 | cat    | 56789   |
| 105 |       6 | dog    | 7890    |

如果列属性为主键或者唯一键,在插入时重复了怎么办?

1.1.2 插入否则更新

语法

INSERT ... ON DUPLICATE KEY UPDATE
column = value [, column = value] ...

其实可以理解为当写好了一个插入语句之后

加上 on duplicate key update 列名1=xx,列名2=xx;

-->表中 stu_num已经有一个是4的了

mysql> insert into t10 (stu_num,name,qq) values (4,'sunnyboy','25679') on duplicate key update id=4,name='sunnyboy';
Query OK, 2 rows affected (0.00 sec)

--> 如果冲突了 就把 id改成4 name改成sunnyboy

mysql> select * from t10;
+-----+---------+----------+---------+
| id  | stu_num | name     | qq      |
+-----+---------+----------+---------+
|   4 |       4 | sunnyboy | 5678    |
| 100 |       1 | steven   | 12345   |
| 101 |       2 | kim      | 1234567 |
| 102 |       3 | kim      | 1234567 |
| 104 |       5 | cat      | 56789   |
| 105 |       6 | dog      | 7890    |
+-----+---------+----------+---------+

 可以根据mysql的回显查看是不是有冲突

Query OK, 2 rows affected (0.47 sec)
-- 0 row affected: 表中有冲突数据,但冲突数据的值和 update 的值相等
-- 1 row affected: 表中没有冲突数据,数据被插入
-- 2 row affected: 表中有冲突数据,并且数据已经被更新

1.2 replace

使用和insert一模一样,只是

  • 主键或者唯一键没有冲突 则直接插入。
  • 逐渐或者唯一键有冲突,则删除后再插入。
mysql> replace into t10 values(106,7,'sam','2369');
Query OK, 1 row affected (0.01 sec)

mysql> select * from t10;
+-----+---------+-----------+---------+
| id  | stu_num | name      | qq      |
+-----+---------+-----------+---------+
|   4 |       4 | sunnyboy  | 5678    |
| 105 |       6 | dog       | 7890    |
| 106 |       7 | sam       | 2369    |
+-----+---------+-----------+---------+

mysql> replace into t10 values(106,7,'sam','123659');
mysql> select * from t10;
+-----+---------+-----------+---------+
| id  | stu_num | name      | qq      |
+-----+---------+-----------+---------+
|   4 |       4 | sunnyboy  | 5678    |
| 105 |       6 | dog       | 7890    |
| 106 |       7 | sam       | 123659  |
+-----+---------+-----------+---------+

2.Retrieve

Retrieve 依赖于 select,where,order by这些关键字。

2.1 select

语法

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 '英语成绩'
    -> );
Query OK, 0 rows affected (0.01 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);

2.1.1 全列查询

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 |
+----+-----------+---------+------+---------+

2.1.2 指定列查询

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 |
+----+-----------+------+

2.1.3 查询字段为表达式

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 |
+----+-----------+---------+
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 |
+----+-----------+----------------------+

2.1.4 为查询结果指定名称

mysql会根据表达式的内容直接成为列名,但是这样不方便。所以提供了as关键字

mysql> select id,name,math+chinese+english as total from exam_result;
+----+-----------+-------+
| id | name      | total |
+----+-----------+-------+
|  1 | 唐三藏    |   221 |
|  2 | 孙悟空    |   242 |
|  3 | 猪悟能    |   276 |
|  4 | 曹孟德    |   233 |
|  5 | 刘玄德    |   185 |
|  6 | 孙权      |   221 |
|  7 | 宋公明    |   170 |
+----+-----------+-------+

把as省略也可以

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 |
+----+-----------+-------+

 2.1.5 去重

distinct 关键字

exam_result中有两个数学成绩为98分的,如果需要去重查看则需要

mysql> select distinct math from exam_result;
+------+
| math |
+------+
|   98 |
|   78 |
|   84 |
|   85 |
|   73 |
|   65 |
+------+

2.2 where

where 用于筛选数据条件,可以用以下运算符进行修改数据选择范围。

>,>=,<=,< 大小比较
=,<=>

等于,=不能用于比较NULL

<=>可以用于比较NULL

!=,<> 不等于
between 0 and 100 在[0,100]之中
in(xxx...) 是否在括号内的内容当中

is null

is not null

是null

不是null

like

模糊匹配

like xx%   %表示任意多个任意字符

like xx_  _表示一个任意字符

and         多个条件表必须都为true
or 任意一个条件为true
not

2.2.1  >  and >= and < and <= and =

mysql> select name,chinese from exam_result where chinese>70;
+-----------+---------+
| name      | chinese |
+-----------+---------+
| 孙悟空    |      87 |
| 猪悟能    |      88 |
| 曹孟德    |      82 |
| 宋公明    |      75 |
+-----------+---------+
mysql> select name,chinese from exam_result where chinese=70;
+--------+---------+
| name   | chinese |
+--------+---------+
| 孙权   |      70 |
+--------+---------+
mysql> select name,chinese from exam_result where chinese!=70;
+-----------+---------+
| name      | chinese |
+-----------+---------+
| 唐三藏    |      67 |
| 孙悟空    |      87 |
| 猪悟能    |      88 |
| 曹孟德    |      82 |
| 刘玄德    |      55 |
| 宋公明    |      75 |
+-----------+---------+

2.2.2  in  between

mysql> select 1 in (1,2,3,4,5);
+------------------+
| 1 in (1,2,3,4,5) |
+------------------+
|                1 |
+------------------+
1 row in set (0.00 sec)

mysql> select 10 in (1,2,3,4,5);
+-------------------+
| 10 in (1,2,3,4,5) |
+-------------------+
|                 0 |
+-------------------+
1 row in set (0.00 sec)
mysql> select name,math from exam_result where math in (98,90,85,84);
+-----------+------+
| name      | math |
+-----------+------+
| 唐三藏    |   98 |
| 猪悟能    |   98 |
| 曹孟德    |   84 |
| 刘玄德    |   85 |
+-----------+------+
4 rows in set (0.00 sec)

mysql> select name,math from exam_result where math between 84 and 98;
+-----------+------+
| name      | math |
+-----------+------+
| 唐三藏    |   98 |
| 猪悟能    |   98 |
| 曹孟德    |   84 |
| 刘玄德    |   85 |
+-----------+------+

2.2.3 查找列数值为null的方法

mysql> select * from 表名 where 列名<=>NULL;
mysql> select * from 表名 where 列名 is NULL;

2.2.4 like 

比如查找姓孙的同学,或者叫孙xx 或者 孙x的同学。

姓孙的条件比较宽松,不限制名字长度,适合使用%,

而孙xx或者孙x,不仅限制了姓,还限制了名字长度,就需要使用_

mysql> select name,math from exam_result where name like '孙%';
+-----------+------+
| name      | math |
+-----------+------+
| 孙悟空    |   78 |
| 孙权      |   73 |
+-----------+------+
2 rows in set (0.00 sec)

mysql> select name,math from exam_result where name like '孙_';
+--------+------+
| name   | math |
+--------+------+
| 孙权   |   73 |
+--------+------+
1 row in set (0.00 sec)

mysql> select name,math from exam_result where name like '孙__';
+-----------+------+
| name      | math |
+-----------+------+
| 孙悟空    |   78 |
+-----------+------+
1 row in set (0.00 sec)

2.2.5 where中使用表达式

  • WHERE 条件中使用表达式

-- 别名不能用在 WHERE 条件中   后面在总结mysql语句的执行顺序时会谈到

mysql> select id,name,chinese+math+english as 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 id,name,chinese+math+english as total from exam_result where chinese+math+english>230;
+----+-----------+-------+
| id | name      | total |
+----+-----------+-------+
|  2 | 孙悟空    |   242 |
|  3 | 猪悟能    |   276 |
|  4 | 曹孟德    |   233 |
+----+-----------+-------+
3 rows in set (0.00 sec)

mysql> select id,name,chinese+math+english as total from exam_result where total>230;
ERROR 1054 (42S22): Unknown column 'total' in 'where clause'
  • WHERE 条件中比较运算符两侧都是字段
mysql> select id,name,chinese,english from exam_result where chinese > english;
+----+-----------+---------+---------+
| id | name      | chinese | english |
+----+-----------+---------+---------+
|  1 | 唐三藏    |      67 |      56 |
|  2 | 孙悟空    |      87 |      77 |
|  4 | 曹孟德    |      82 |      67 |
|  5 | 刘玄德    |      55 |      45 |
|  7 | 宋公明    |      75 |      30 |
+----+-----------+---------+---------+
  •  and 和 not的使用

 查询语文成绩大于80 并且不姓孙的同学

mysql> select id,name,chinese from exam_result where chinese>80;
+----+-----------+---------+
| id | name      | chinese |
+----+-----------+---------+
|  2 | 孙悟空    |      87 |
|  3 | 猪悟能    |      88 |
|  4 | 曹孟德    |      82 |
+----+-----------+---------+
3 rows in set (0.00 sec)

mysql> select id,name,chinese from exam_result where chinese>80 and name not like '孙%';
+----+-----------+---------+
| id | name      | chinese |
+----+-----------+---------+
|  3 | 猪悟能    |      88 |
|  4 | 曹孟德    |      82 |
+----+-----------+---------+
  • 综合查询

孙某同学,否则要求总成绩 > 200 并且 语文成绩 < 数学成绩 并且 英语成绩 > 80

mysql> select id,name,chinese+math+english as total from exam_result;
+----+-----------+-------+
| id | name      | total |
+----+-----------+-------+
|  1 | 唐三藏    |   221 |
|  2 | 孙悟空    |   242 |
|  3 | 猪悟能    |   276 |
|  4 | 曹孟德    |   233 |
|  5 | 刘玄德    |   185 |
|  6 | 孙权      |   221 |
|  7 | 宋公明    |   170 |
+----+-----------+-------+
mysql> select id,name,chinese+math+english as total from exam_result where name like '孙_' or (math+english+chinese>200 and chinese<math and english>80) ;
+----+-----------+-------+
| id | name      | total |
+----+-----------+-------+
|  3 | 猪悟能    |   276 |
|  6 | 孙权      |   221 |
+----+-----------+-------+

2.3 结果排序

asc为升序 desc为降序  默认为升序。 书写位置在筛选完成之后。

注意:null视为比任何元素都小。

SELECT ... FROM table_name [WHERE ...]
ORDER BY column [ASC|DESC], [...];

mysql> select id,name,math from exam_result order by math asc;
+----+-----------+------+
| id | name      | math |
+----+-----------+------+
|  7 | 宋公明    |   65 |
|  6 | 孙权      |   73 |
|  2 | 孙悟空    |   78 |
|  4 | 曹孟德    |   84 |
|  5 | 刘玄德    |   85 |
|  1 | 唐三藏    |   98 |
|  3 | 猪悟能    |   98 |
+----+-----------+------+
7 rows in set (0.00 sec)

mysql> select id,name,math from exam_result order by math desc;
+----+-----------+------+
| id | name      | math |
+----+-----------+------+
|  1 | 唐三藏    |   98 |
|  3 | 猪悟能    |   98 |
|  5 | 刘玄德    |   85 |
|  4 | 曹孟德    |   84 |
|  2 | 孙悟空    |   78 |
|  6 | 孙权      |   73 |
|  7 | 宋公明    |   65 |
+----+-----------+------+
mysql> select id,name,math from exam_result where math>80 order by math desc;
+----+-----------+------+
| id | name      | math |
+----+-----------+------+
|  1 | 唐三藏    |   98 |
|  3 | 猪悟能    |   98 |
|  5 | 刘玄德    |   85 |
|  4 | 曹孟德    |   84 |
+----+-----------+------+

2.3.2 多级排序

order by 后面跟的越近,排序时的优先级越高

数学成绩一样,那就按语文成绩来排。

注意每一项都需要带上desc 或者 asc 不然可能由于缺省为asc的原因导致排序不是理想结果。

mysql> select id,name,math,chinese from exam_result where math>80 order by math desc,chinese desc;
+----+-----------+------+---------+
| id | name      | math | chinese |
+----+-----------+------+---------+
|  3 | 猪悟能    |   98 |      88 |
|  1 | 唐三藏    |   98 |      67 |
|  5 | 刘玄德    |   85 |      55 |
|  4 | 曹孟德    |   84 |      82 |
+----+-----------+------+---------+
4 rows in set (0.00 sec)

mysql> select id,name,math,chinese from exam_result where math>80 order by math desc,chinese asc;
+----+-----------+------+---------+
| id | name      | math | chinese |
+----+-----------+------+---------+
|  1 | 唐三藏    |   98 |      67 |
|  3 | 猪悟能    |   98 |      88 |
|  5 | 刘玄德    |   85 |      55 |
|  4 | 曹孟德    |   84 |      82 |
+----+-----------+------+---------+

2.3.3 显示多个或者有限个

order by后面可以跟上

  • limit 数字 以限制显示的个数
  • 也可以 limit s,n  

s表示从哪里开始 n表示要几个 

s为0表示从最大/小的 开始     s为1表示从第二大/小的开始

mysql> select id,name,math+chinese+english as grade from exam_result where chinese+math+english>210 order by grade asc;
+----+-----------+-------+
| id | name      | grade |
+----+-----------+-------+
|  1 | 唐三藏    |   221 |
|  6 | 孙权      |   221 |
|  4 | 曹孟德    |   233 |
|  2 | 孙悟空    |   242 |
|  3 | 猪悟能    |   276 |
+----+-----------+-------+
5 rows in set (0.00 sec)

mysql> select id,name,math+chinese+english as grade from exam_result where chinese+math+english>210 order by grade asc limit 3;
+----+-----------+-------+
| id | name      | grade |
+----+-----------+-------+
|  1 | 唐三藏    |   221 |
|  6 | 孙权      |   221 |
|  4 | 曹孟德    |   233 |
+----+-----------+-------+

2.3.4 补充

order by也可以跟表达式 而且在order by中可以使用别名

mysql> select id,name,math+chinese as grade from exam_result where chinese+math>140 order by grade asc;
+----+-----------+-------+
| id | name      | grade |
+----+-----------+-------+
|  6 | 孙权      |   143 |
|  1 | 唐三藏    |   165 |
|  2 | 孙悟空    |   165 |
|  4 | 曹孟德    |   166 |
|  3 | 猪悟能    |   186 |
+----+-----------+-------+

2.4 mysql中select语句的执行顺序

首先,mysql在执行select语句时,一定首先要知道在哪个表里取数据。from语句一定是首先执行的。然后如果有where语句,mysql就会把在条件外的数据筛掉。然后根据select 后面跟的列名,把表的数据显示到屏幕上。

  • 所以如果重命名是在where语句之后进行的,where语句从哪里知道这个别名呢?这也就是为什么where语句中不能使用别名的原因。
  • 如果是order by,我们既然已经开始对数据进行排序了。那么首先是不是我们已经拿到了数据,也就是没有排序之前的数据,那这个时候,重命名已经执行好了,order by语句中是可以认识这个别名的,所以也就可以使用别名。
  • group by 的意思是根据组别先筛选出一些数据,不是该类别的不要。那他的执行顺序是不是按理来说比where更早呢?因为如果先执行where,会将一部分我不要的数据也筛选进来。

3. update

update操作依赖于update关键字

UPDATE table_name SET column = expr [, column = expr ...]
[WHERE ...] [ORDER BY ...] [LIMIT ...]

 3.1 更新数据

  • 更新一下数学成绩
mysql> select * from exam_result where name='曹孟德';
+----+-----------+---------+------+---------+
| id | name      | chinese | math | english |
+----+-----------+---------+------+---------+
|  4 | 曹孟德    |      82 |   84 |      67 |
+----+-----------+---------+------+---------+
1 row in set (0.00 sec)

mysql> update exam_result set math=90 where name='曹孟德';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from exam_result where name='曹孟德';
+----+-----------+---------+------+---------+
| id | name      | chinese | math | english |
+----+-----------+---------+------+---------+
|  4 | 曹孟德    |      82 |   90 |      67 |
+----+-----------+---------+------+---------+
  • 可以一次性多次修改

mysql> update exam_result set math=90,chinese=80 where name='曹孟德';

  •  综合筛选条件进行修改

将总成绩倒数前三的 3 位同学的数学成绩加上 30 分

mysql> select id,name,chinese+math+english as total from exam_result order by total asc limit 3;
+----+-----------+-------+
| id | name      | total |
+----+-----------+-------+
|  7 | 宋公明    |   170 |
|  5 | 刘玄德    |   185 |
|  1 | 唐三藏    |   221 |
+----+-----------+-------+
3 rows in set (0.00 sec)

mysql> update exam_result set math=math+30 order by chinese+math+english limit 3;
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3  Changed: 3  Warnings: 0

mysql> select id,name,chinese+math+english as total from exam_result order by total asc limit 3;
+----+-----------+-------+
| id | name      | total |
+----+-----------+-------+
|  7 | 宋公明    |   200 |
|  5 | 刘玄德    |   215 |
|  6 | 孙权      |   221 |
+----+-----------+-------+
  • 注意没有where 或者 order by limit语句 update会更新全表的内容
     

 4. delete操作

delete操作依赖于delete关键字  和 truncate 关键字

DELETE FROM table_name [WHERE ...] [ORDER BY ...] [LIMIT ...]     

 4.1 delete

4.1.1 删除某一个数据

mysql> DELETE FROM exam_result WHERE name = '孙悟空';

 4.1.2 删除整张表的数据

mysql> DELETE FROM exam_result;

 Delete 跟表名会删除所有数据,但是会自增长值会依然保留。

 4.2 truncate

  • 只能对整表操作,不能像 DELETE 一样针对部分数据操作;
  • 实际上 MySQL 不对数据操作,所以比 DELETE 更快,但是TRUNCATE在删除数据的时候,并不经过真正的事物,所以无法回滚。
  • 会重置 AUTO_INCREMENT 项
     

 5.插入查询结果

就是先用select查出来一些数据,然后insert到另一个表里面,这就是插入查询结果。

比如:删除表中的重复记录,重复的数据只能有一份。

(1)先创建一个oldtable,向里面插入一些数据

mysql> select * from oldtable;
+------+------+
| id   | name |
+------+------+
|  100 | aaa  |
|  100 | aaa  |
|  200 | bbb  |
|  200 | bbb  |
|  300 | acb  |
|  300 | acb  |
+------+------+

(2)创建一个表,使用like语句以获得和目标对象一致的列属性

mysql> create table newtable like oldtable;
Query OK, 0 rows affected (0.01 sec)

mysql> desc newtable;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | YES  |     | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+

(3)把oldtable里面的数据去重后插入

mysql> insert into newtable select distinct * from oldtable;
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from newtable;
+------+------+
| id   | name |
+------+------+
|  100 | aaa  |
|  200 | bbb  |
|  300 | acb  |
+------+------+

(4) 将newtable重命名为oldtable文章来源地址https://www.toymoban.com/news/detail-430712.html

mysql> rename table oldtable to fordelete_table,newtable to oldtable;

mysql> show tables;
+-----------------+
| Tables_in_test1 |
+-----------------+
| exam_result     |
| fordelete_table |
| myclass         |
| oldtable        |
| person          |

到了这里,关于MySQL--表的基本查询--0410--15的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【lesson14】MySQL表的基本查询(1)

    CRUD : Create(创建), Retrieve(读取),Update(更新),Delete(删除) 建表 基本测试 插入数据 全列查询 指定列查询 select后面跟的是表达式 为查询结果指定别名 不起别名的效果 其中as可以省略 我们看到也是可以用汉字起别名的 对查询结果去重 我们看到这里确实重复了 用distinct去重

    2024年02月04日
    浏览(30)
  • 【MySQL】基本查询之表的增删改查

    CRUD : Create(创建), Retrieve(读取),Update(更新),Delete(删除) 创建user表并设置四个字段 注意:insert into可以省略成 insert 由于 主键 或者 唯一键 对应的值已经存在而导致插入失败,可以选择性的进行同步更新操作 注意:更新的数据也不能和其他数据冲突 创建数据表并添加数据

    2024年02月17日
    浏览(37)
  • 【MySQL】表的增删改查——MySQL基本查询、数据库表的创建、表的读取、表的更新、表的删除

         CURD是一个数据库技术中的缩写词,它代表Create(创建),Retrieve(读取),Update(更新),Delete(删除)操作。 这四个基本操作是数据库管理的基础,用于处理数据的基本原子操作。      在MySQL中,Create操作是十分重要的,它帮助用于创建数据库对象,如数据

    2024年03月18日
    浏览(45)
  • 【MySQL】insert和select单表查询详解(包含大量示例,看了必会)

    我前面的博客算是把sql中所有的DDL讲完了,下面就来讲讲关于数据操作的语言,也就是DML。主要就是对表中数据做增删查改的操作。 本篇主要对DML中的增和查进行讲解,下一篇会对DML中的删和改进行讲解。 四大块:CRUD : Create(创建), Retrieve(读取),Update(更新),Delete(删除),

    2024年02月05日
    浏览(36)
  • 【MySQL学习】MySQL表的复合查询

    对MySQL表的基本查询还远远达不到实际开发过程中的需求,因此还需要掌握对数据库表的复合查询。本文介绍了多表查询、子查询、自连接、内外连接等复合查询的案例。 来自oracle 9i的经典测试表: emp员工表 dept部门表 salgrade工资等级表 MySQL表的基本查询都是针对一张表进行

    2024年02月03日
    浏览(25)
  • pycharm连接MySql数据库,新建表creat table、删除表drop table、查询表select、插入数据insert

    亲爱的小伙伴,欢迎你来为学习新的知识啦~~~ 一、安装pymysql 首先我们肯定要先在python中把能操作mySql的第三方数据库安装一下呀~ 安装方式:pip安装(你有没有安装好pip呀,可以看安装pip的相关文章进行安装好哦~) 打开我们电脑的终端,然后输入以下的命令,将我们

    2024年02月09日
    浏览(39)
  • 【MySQL】_4.MySQL表的设计与聚合查询

    目录 1. 表的设计 2. 查询与新增的联合 3. 聚合查询 3.1 聚合查询函数 3.1.1  COUNT函数 3.1.2 SUM函数 3.1.3 AVG 函数  3.1.4 MIN函数 与 MAX函数 3.2 GROUP BY子句 3.3 HAVING  (一)、梳理清楚需求中的“实体”: (1)数据库中的实体类比面向对象的对象; (2)很多时候每个实体都需要对

    2024年02月16日
    浏览(23)
  • mysql数据库表的多条件查询

    select可以返回多条数据也可以返回一条数据 如果要查询所有的字段可以用 *****代替 where后面跟的是筛选条件(可选) N 是返回的数据条数(可选) M 是数据的偏移量(可选) 例如:在职位招聘表中获取在长沙雨花区的前10条信息 通用语法 : 询语句中你可以使用一个或者多个

    2024年02月11日
    浏览(36)
  • 【MySQL】表的基本操作

    数据类型 大小 说明 bit[(M)] M指定位数,默认为1 二进制,M范围从1到64,存储数值范围从0到2^M-1 tinyint 1字节 smallint 2字节 int 4字节 bigint 8字节 float(M,D) 4字节 单精度,M指定长度,D 指定小数位数,会发生精度丢失 double(M,D) 8字节 decimal(M,D) M/D最大值+2 双精度,M指定长度,D表示小

    2023年04月22日
    浏览(32)
  • [MySQL]基本数据类型及表的基本操作

    哈喽,大家好!我是保护小周ღ,本期为大家带来的是 MySQL 数据库常用的数据类型,数据表的基本操作:创建、删除、修改表,针对修改表的结构进行了讲解,随后是如何向数据表中添加数据,浅浅的提了一下表中数据的查询,更多相关知识敬请期待:保护小周ღ *★,°*:.☆

    2024年02月02日
    浏览(36)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包