SQLite - INSERT INTO SELECT - 如何插入 "join of 3 existing tables into a new table" 的数据?

SQLite - INSERT INTO SELECT - how to insert data of "join of 3 existing tables into a new table"?

所以这里的场景是,我在数据库中有 4 个 table,即:

  1. "question_info":
    创建 TABLE <code>question_info ( q_id mediumint(9) 不为空, q_type_id int(11) 不为空, q_options_id mediumint(9) 不为空, q_category_id int(11) 不为空, q_text varchar(2048) 不为空, status tinyint(4) NOT NULL DEFAULT '0', q_date_added date NOT NULL DEFAULT '2013-01-01', q_difficulty_level tinyint(4) NOT NULL DEFAULT '0', 主键(q_id) );

  2. "question_options_info":
    创建 TABLE <code>question_options_info ( q_options_id mediumint(9) 不为空, q_options_1 varchar(255) 不为空, q_options_2 varchar(255) 不为空, q_options_3 varchar(255) 不为空, q_options_4 varchar(255) 不为空, q_options_ex_1 varchar(1024) 默认为空, q_options_ex_2 varchar(1024) 默认为空, q_options_ex_3 varchar(1024) 默认为空, q_options_ex_4 varchar(1024) 默认为空, 主键(q_options_id) );

  3. "question_answer_info":
    创建 TABLE <code>question_answer_info ( q_id mediumint(9) 不为空, q_options mediumint(9) 不为空 );

  4. "trivia_data":
    创建 TABLE <code>trivia_data ( q_id mediumint(9) 不为空, q_text varchar(2048) 不为空, q_options_1 varchar(255) 不为空, q_options_2 varchar(255) 不为空, q_options_3 varchar(255) 不为空, q_options_4 varchar(255) 不为空, q_options mediumint(9) 不为空, q_difficulty_level tinyint(4) NOT NULL DEFAULT '0', q_date_added date NOT NULL DEFAULT '2015-04-8', 主键(q_id) );

所以我需要的是,将数据插入 trivia_data table。 数据由这个查询return编辑:

SELECT question_info.q_id, question_info.q_text, question_options_info .q_options_1, question_options_info.q_options_2, question_options_info.q_options_3, question_options_info.q_options_4, question_answer_info.q_options, question_info.q_difficulty_level, question_info.q_date_added<br> FROM question_info JOIN question_options_info ON question_info.q_options_id = question_options_info.q_options_id JOIN question_answer_info ON question_info.q_id = question_answer_info.q_id;

此查询将 return 数据有点像这样:

我已经尝试过这个特定的查询来插入数据:
INSERT INTO trivia_data VALUES(q_id, q_text, q_options_1, q_options_2, q_options_3, q_options_4, q_options, q_difficulty_level, q_date_added) SELECT question_info.q_id, question_info.q_text, question_options_info.q_options_1, question_options_info.q_options_2, question_options_info.q_options_3, question_options_info.q_options_4, question_answer_info.q_options, question_info.q_difficulty_level, question_info.q_date_added FROM question_info JOIN question_options_info on question_info.q_options_id = question_options_info.q_options_id JOIN question_answer_info on question_info.q_id = question_answer_info.q_id;

但它总是 return 这个错误:
near "SELECT": syntax error:

老实说我是SQL的新手。所以请尽量简单地解释一下。 任何帮助,将不胜感激。 谢谢。

您不需要 VALUES 关键字,因为您正在从查询中选择:

INSERT INTO trivia_data (
    q_id, 
    q_text, 
    q_options_1, 
    q_options_2, 
    q_options_3, 
    q_options_4, 
    q_options, 
    q_difficulty_level, 
    q_date_added)  
SELECT 
    question_info.q_id, 
    question_info.q_text, 
    question_options_info.q_options_1, 
    question_options_info.q_options_2, 
    question_options_info.q_options_3, 
    question_options_info.q_options_4, 
    question_answer_info.q_options, 
    question_info.q_difficulty_level, 
    question_info.q_date_added 
FROM question_info 
    JOIN question_options_info on question_info.q_options_id = question_options_info.q_options_id 
    JOIN question_answer_info on question_info.q_id = question_answer_info.q_id;

一般来说,如果您要插入一条记录,则语法为

INSERT INTO <tablename> (<column1>, <column2>, ..., <columnN>)
VALUES (<value1>, <value2>, ..., <valueN>)

如果要插入结果,语法如下:

INSERT INTO <tablename> (<column1>, <column2>, ..., <columnN>)
SELECT <value1>, <value2>, ..., <valueN> FROM ...

如您所见,在这种情况下没有 VALUES 关键字

从您的 SQL 中删除 VALUES,因为在这种情况下值来自 SELECT。

INSERT INTO trivia_data (
  q_id,
  q_text,
  q_options_1,
  q_options_2,
  q_options_3,
  q_options_4,
  q_options,
  q_difficulty_level,
  q_date_added
)
SELECT
  question_info.q_id,
  question_info.q_text,
  question_options_info.q_options_1,
  question_options_info.q_options_2,
  question_options_info.q_options_3,
  question_options_info.q_options_4,
  question_answer_info.q_options,
  question_info.q_difficulty_level,
  question_info.q_date_added
FROM question_info
JOIN question_options_info
  ON question_info.q_options_id = question_options_info.q_options_id
JOIN question_answer_info
  ON question_info.q_id = question_answer_info.q_id;

删除 VALUES 关键字。试试这个:

INSERT INTO trivia_data (q_id, q_text, q_options_1, q_options_2, 
    q_options_3, q_options_4, q_options, q_difficulty_level, q_date_added)  
SELECT question_info.q_id, question_info.q_text, 
    question_options_info.q_options_1, question_options_info.q_options_2, 
    question_options_info.q_options_3, question_options_info.q_options_4, 
    question_answer_info.q_options, question_info.q_difficulty_level, 
    question_info.q_date_added FROM question_info 
    JOIN question_options_info on question_info.q_options_id = question_options_info.q_options_id 
    JOIN question_answer_info on question_info.q_id = question_answer_info.q_id;