像以下表中插入數據:mysql
id是自動增加的主鍵。sql
INSERT INTO student_score (student_name, score, SUBJECT) VALUES ('張三', 90, '語文');
結果以下:測試
mysql> SELECT * FROM student_score; +----+--------------+-------+---------+ | id | student_name | score | subject | +----+--------------+-------+---------+ | 1 | 張三 | 90 | 語文 | +----+--------------+-------+---------+ 1 row in set
INSERT INTO student_score (student_name, score, SUBJECT) VALUES ('李四', 95, '語文'), ('王五', 98, '語文');
結果以下:spa
mysql> SELECT * FROM student_score; +----+--------------+-------+---------+ | id | student_name | score | subject | +----+--------------+-------+---------+ | 1 | 張三 | 90 | 語文 | | 2 | 李四 | 95 | 語文 | | 3 | 王五 | 98 | 語文 | +----+--------------+-------+---------+ 3 rows in set
首先清空student_score表,student_score_record的數據以下:3d
mysql> SELECT * FROM student_score_record; +----+--------------+-------+---------+------------+ | id | student_name | score | subject | test_date | +----+--------------+-------+---------+------------+ | 1 | 張三 | 1 | 語文 | 2020-12-10 | | 2 | 李四 | 2 | 語文 | 2020-12-11 | | 3 | 王五 | 3 | 語文 | 2020-12-11 | +----+--------------+-------+---------+------------+ 3 rows in set
現須要將student_score_record的相關數據插入到student_score中:code
INSERT INTO student_score (student_name, score, SUBJECT) SELECT student_name, score, SUBJECT FROM student_score_record;
結果以下:blog
mysql> SELECT * FROM student_score; +----+--------------+-------+---------+ | id | student_name | score | subject | +----+--------------+-------+---------+ | 7 | 張三 | 1 | 語文 | | 8 | 李四 | 2 | 語文 | | 9 | 王五 | 3 | 語文 | +----+--------------+-------+---------+ 3 rows in set
首先清空student_score表,student表的數據以下:class
mysql> SELECT * FROM `student`; +----+--------------+------------+ | id | student_name | student_id | +----+--------------+------------+ | 1 | 張三 | 1 | | 2 | 李四 | 2 | | 3 | 王五 | 3 | +----+--------------+------------+ 3 rows in set
student_id_score_record表的數據以下:test
mysql> SELECT * FROM `student_id_score_record`; +----+------------+-------+---------+ | id | student_id | score | subject | +----+------------+-------+---------+ | 10 | 1 | 11 | 語文 | | 11 | 2 | 12 | 語文 | | 12 | 3 | 13 | 語文 | +----+------------+-------+---------+ 3 rows in set
如今將student和student_id_score_record的關聯信息插入到student_score表中:date
INSERT INTO student_score (student_name, score, SUBJECT) SELECT student.student_name, student_id_score_record.score, student_id_score_record.`subject` FROM student INNER JOIN student_id_score_record ON student.id = student_id_score_record.student_id;
結果以下:
mysql> SELECT * FROM student_score; +----+--------------+-------+---------+ | id | student_name | score | subject | +----+--------------+-------+---------+ | 10 | 張三 | 11 | 語文 | | 11 | 李四 | 12 | 語文 | | 12 | 王五 | 13 | 語文 | +----+--------------+-------+---------+ 3 rows in set
仍是四中的例子,不過此次換種寫法(此寫法主要目的是測試將幾個獨立表的不一樣字段插入同一個表時如何處理):
INSERT INTO student_score (student_name, score, SUBJECT) VALUES ( ( SELECT student_name FROM student WHERE student_id = 1 ), ( SELECT score FROM student_id_score_record WHERE student_id = 1 ), '語文' );
結果以下:
mysql> SELECT * FROM student_score; +----+--------------+-------+---------+ | id | student_name | score | subject | +----+--------------+-------+---------+ | 14 | 張三 | 11 | 語文 | +----+--------------+-------+---------+ 1 row in set