1. 程式人生 > 實用技巧 >Quick之QML自定義視窗標題欄

Quick之QML自定義視窗標題欄

技術標籤:資料庫資料探勘大資料資料分析人工智慧

一 普通單條插入:

像如下表中插入資料:

id是自動增長的主鍵。

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, '語文');

結果如下:

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的資料如下:

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中:

INSERT INTO student_score (student_name, score, SUBJECT) SELECT
    student_name,
    score,
    SUBJECT
FROM
    student_score_record;

結果如下:

mysql> SELECT * FROM student_score;
+----+--------------+-------+---------+
| id | student_name | score | subject |
+----+--------------+-------+---------+
|  7 | 張三         |     1 | 語文    |
|  8 | 李四         |     2 | 語文    |
|  9 | 王五         |     3 | 語文    |
+----+--------------+-------+---------+
3 rows in set

四 將資料從多個關聯表插入一個表

首先清空student_score表,student表的資料如下:

mysql> SELECT * FROM `student`;
+----+--------------+------------+
| id | student_name | student_id |
+----+--------------+------------+
|  1 | 張三         |          1 |
|  2 | 李四         |          2 |
|  3 | 王五         |          3 |
+----+--------------+------------+
3 rows in set

student_id_score_record表的資料如下:

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表中:

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