SQLite 使用 SELECT 中的插入手动设置列
SQLite manually set column with insert from a SELECT
我需要在我的数据库中创建一个禁止列表。在你问之前,不,它实际上不是为了书籍,禁止书籍是愚蠢的。为此,我根据 table 中已有的标准进行选择,然后直接插入禁令 table。问题是,我想包含一个手动字符串来说明禁令的原因。我在下面包含了一些示例数据和我目前正在使用的 SQL。
示例数据:
书籍
ID | Author | Title
1 | Mark Twain | Huckleberry Fin
2 | Stephen King | IT
3 | Mark Twain | Tom Sawyer
4 | Stephen King | Carrie
5 | William Shakespeare| Hamlet
6 | Stephen King | The Stand
7 | Ernest Hemingway | The Old Man and the Sea
8 | JK Rowling | Harry Potter
9 | Ernest Hemingway | For Whom the Bell Tolls
10 | William Shakespeare| Othello
11 | Ernest Hemingway | A Farewell to Arms
12 | William Shakespeare| Richard III
13 | JK Rowling | Fantastic Beasts and where to find them
禁令
ID | Reason
8 | Magic & Stuff
13 | Magic & Stuff
当前查询:
INSERT OR IGNORE INTO Bans SELECT ID FROM Books
WHERE Author = "Stephen King"
此查询会将 Stephan Kings 书籍的所有 ID 放入禁书 table。但是,我希望能够给出他们被禁止的原因。
当前查询结果:
禁令
ID | Reason
8 | Magic & Stuff
13 | Magic & Stuff
2 |
4 |
6 |
当前查询的期望结果:
我想要做的是有一个查询,让我也能给出一个理由。所以我最终得到了这个
禁令
ID | Reason
8 | Magic & Stuff
13 | Magic & Stuff
2 | Too Scary For Kids
4 | Too Scary For Kids
6 | Too Scary For Kids
如何做到这一点?
INSERT OR IGNORE INTO Bans
SELECT ID, "Too Scary For Kids" FROM Books WHERE Author = "Stephen King"
我需要在我的数据库中创建一个禁止列表。在你问之前,不,它实际上不是为了书籍,禁止书籍是愚蠢的。为此,我根据 table 中已有的标准进行选择,然后直接插入禁令 table。问题是,我想包含一个手动字符串来说明禁令的原因。我在下面包含了一些示例数据和我目前正在使用的 SQL。
示例数据:
书籍
ID | Author | Title
1 | Mark Twain | Huckleberry Fin
2 | Stephen King | IT
3 | Mark Twain | Tom Sawyer
4 | Stephen King | Carrie
5 | William Shakespeare| Hamlet
6 | Stephen King | The Stand
7 | Ernest Hemingway | The Old Man and the Sea
8 | JK Rowling | Harry Potter
9 | Ernest Hemingway | For Whom the Bell Tolls
10 | William Shakespeare| Othello
11 | Ernest Hemingway | A Farewell to Arms
12 | William Shakespeare| Richard III
13 | JK Rowling | Fantastic Beasts and where to find them
禁令
ID | Reason
8 | Magic & Stuff
13 | Magic & Stuff
当前查询:
INSERT OR IGNORE INTO Bans SELECT ID FROM Books
WHERE Author = "Stephen King"
此查询会将 Stephan Kings 书籍的所有 ID 放入禁书 table。但是,我希望能够给出他们被禁止的原因。
当前查询结果:
禁令
ID | Reason
8 | Magic & Stuff
13 | Magic & Stuff
2 |
4 |
6 |
当前查询的期望结果:
我想要做的是有一个查询,让我也能给出一个理由。所以我最终得到了这个
禁令
ID | Reason
8 | Magic & Stuff
13 | Magic & Stuff
2 | Too Scary For Kids
4 | Too Scary For Kids
6 | Too Scary For Kids
如何做到这一点?
INSERT OR IGNORE INTO Bans
SELECT ID, "Too Scary For Kids" FROM Books WHERE Author = "Stephen King"