1、第一种:INSERT [INTO] tbl_name[ (col_name, ... ) ] {VALUES | VALUE}({expr |default}, ... ), (...), ...
如果为自动编号的字段赋值的话,可以采用NULL或者DEFAULT让其采用默认的递增的形式来实现。
INSERT users VALUES(DEFAULT, 'Tom', '123', 25, 1);
INSERT users VALUES(NULL, 'John', '456', 3*7-5, 1);
INSERT users VALUES(NULL, 'John', '456', DEFAULT, 1);
INSERT users VALUES(DEFAULT, 'John', '456', 3*7-5, 1), (NULL, 'ROSE',67, DEFAULT, 0);
2、第二种:INSERT [INTO] tbl_name SET col_name = {expr |DEFAULT}, ...
与第一种方法的区别在于,此方法可以使用子查询(SubQuery),
一次性只能插入一条记录
INSERT users SET username='Ben', password='456';
多表更新
1、第一种方法
(1)将查询结果写入数据表
INSERT [INTO] tbl_name[(col_name,...)] SELECT...
mysql> SELECT goods_cate FROM tdb_goods GROUP BY goods_cate; #以goods_cate分组
+---------------+
| goods_cate |
+---------------+
| 台式机 |
| 平板电脑 |
| 服务器/工作站 |
| 游戏本 |
| 笔记本 |
| 笔记本配件 |
| 超级本 |
+---------------+
7 rows in set (0.00 sec)
mysql> SHOW COLUMNS FROM tdb_goods_cates; #创建的数据表
+-----------+----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+----------------------+------+-----+---------+----------------+
| cate_id | smallint(5) unsigned | NO | PRI | NULL | auto_increment |
| cate_name | varchar(20) | NO | | NULL | |
+-----------+----------------------+------+-----+---------+----------------+
2 rows in set (0.01 sec)
mysql> INSERT tdb_goods_cates(cate_name) SELECT goods_cate FROM tdb_goods GROUP BY goods_cate; #将查询结果写入数据表
Query OK, 7 rows affected (0.04 sec)
Records: 7 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM tdb_goods_cates;
+---------+---------------+
| cate_id | cate_name |
+---------+---------------+
| 1 | 台式机 |
| 2 | 平板电脑 |
| 3 | 服务器/工作站 |
| 4 | 游戏本 |
| 5 | 笔记本 |
| 6 | 笔记本配件 |
| 7 | 超级本 |
+---------+---------------+
7 rows in set (0.00 sec)
(2)更新数据表
UPDATE table_references
SET col_name1={expr1|DEFAULT}
[, col_name2={expr2|DEFAULT}]...
[WHERE where_condition]
table_references
{[INNER|CROSS] JOIN | {LEFT|RIGHT} [OUTER] JOIN}
oods_cate=cate_id;
Query OK, 22 rows affected (0.06 sec)
Rows matched: 22 Changed: 22 Warnings: 0
修改之前:
*************************** 21. row ***************************
goods_id: 21
goods_name: HMZ-T3W 头戴显示设备
goods_cate: 笔记本配件
brand_name: 索尼
goods_price: 6999.000
is_show: 1
is_saleoff: 0
*************************** 22. row ***************************
goods_id: 22
goods_name: 商务双肩背包
goods_cate: 笔记本配件
brand_name: 索尼
goods_price: 99.000
is_show: 1
is_saleoff: 0
修改之后:
*************************** 21. row ***************************
goods_id: 21
goods_name: HMZ-T3W 头戴显示设备
goods_cate: 6
brand_name: 索尼
goods_price: 6999.000
is_show: 1
is_saleoff: 0
*************************** 22. row ***************************
goods_id: 22
goods_name: 商务双肩背包
goods_cate: 6
brand_name: 索尼
goods_price: 99.000
is_show: 1
is_saleoff: 0
方法2:(1)创建数据表同时将查询结果写入到数据表
CREATE TABLE [IF NOT EXISTS] tbl_name
[(create_definition,...)]
select_statement
mysql> CREATE TABLE tdb_goods_brands(
-> brand_id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
-> brand_name VARCHAR(40) NOT NULL
-> )
-> SELECT brand_name FROM tdb_goods GROUP BY brand_name;
(2)更新数据表
mysql> UPDATE tdb_goods AS g INNER JOIN tdb_goods_brands AS b ON g.brand_name=b.
brand_name SET g.brand_name = b.brand_id;
3、第三种:INSERT [INTO] tbl_name [(col_name, ... )] SELECT ...
说明:此方法可以将查询结果插入到指定数据表
mysql> SELECT * FROM users;
+----+----------+----------+-----+------+
| id | username | password | age | sex |
+----+----------+----------+-----+------+
| 1 | Tom | 123 | 29 | 0 |
| 2 | John | 456 | 38 | 0 |
| 3 | Tom | 123 | 27 | 0 |
| 4 | John | 456 | 27 | 0 |
| 10 | John | 456 | 21 | 0 |
| 11 | ROSE | 67 | 4 | 0 |
| 12 | Ben | 456 | 13 | NULL |
+----+----------+----------+-----+------+
mysql> CREATE TABLE TEST(
-> id TINYINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
-> username VARCHAR(20)
-> );
Query OK, 0 rows affected (0.13 sec)
mysql>
mysql> SELECT * FROM test;
Empty set (0.02 sec)
mysql> INSERT test(username) SELECT username FROM users WHERE age >=30;
Query OK, 1 row affected (0.05 sec)
Records: 1 Duplicates: 0 Warnings: 0
mysql> SELECT * FROM test;
+----+----------+
| id | username |
+----+----------+
| 1 | John |
+----+----------+
1 row in set (0.00 sec)
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:mysql插入记录INSERT与多表更新 - Python技术站