Search code examples
sqliteetl

SQLite manually set column with insert from a SELECT


I need to create a ban list in my DB. Before you ask, no it's not actually for books, banning books is dumb. To do this, I'm selecting based on criteria already in the table and I'm inserting straight into the Bans table. The problem is, I want to include a manual string for the reason for the ban. I've included some sample data below and the SQL I'm using currently.

Sample Data:

Books

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

Bans

ID  | Reason
8   | Magic & Stuff
13  | Magic & Stuff

Current Query:

 INSERT OR IGNORE INTO Bans SELECT ID FROM Books 
 WHERE Author = "Stephen King"

This query will put all the IDs for Stephan Kings books into the bans table. But, I'd like to be able to assign a reason that they were banned.

Result Of Current Query:

Bans

ID  | Reason
8   | Magic & Stuff
13  | Magic & Stuff
2   |
4   |
6   |

Desired Result Of Current Query:

What I want to be able to do is have a query that will allow me to give a reason as well. So I end up with this instead

Bans

ID  | Reason
8   | Magic & Stuff
13  | Magic & Stuff
2   | Too Scary For Kids
4   | Too Scary For Kids
6   | Too Scary For Kids

How can this be done?


Solution

  • INSERT OR IGNORE INTO Bans 
        SELECT ID, "Too Scary For Kids" FROM Books WHERE Author = "Stephen King"