繁体   English   中英

从值插入到MySQL表并选择语句

[英]Insert into MySQL table from value and select statement

我想通过从另一个表中选择字段将值插入一个表中,并添加诸如date和login_id之类的信息(这些都不来自我所选择的表)。

$sql = "INSERT INTO questions_to_answer (login_id, question_id, Question_Category, QuestionType, Question, Meaning) VALUES (?,?,?,?,?,?)";
$stmt= $DB_con->prepare($sql);

$stmt->execute($test_user_id, 
    "SELECT question_id, 
            Question_Category, 
            QuestionType, 
            Question, 
            Meaning all_questions 
      WHERE personality_profile_questions.Question_Category = 1");

您需要在prepare包含SELECT ,在执行中不能包含SQL命令。 如果在执行中,它将被视为文字字符串,用引号引起来并转义。

尝试:

$sql = "INSERT INTO questions_to_answer (login_id, question_id, Question_Category, QuestionType, Question, Meaning) 
        SELECT ?, question_id, Question_Category, QuestionType, Question, Meaning 
        WHERE personality_profile_questions.Question_Category = 1";
$stmt= $DB_con->prepare($sql);
$stmt->execute(array($test_user_id));

假设$test_user_id = login_id 我不清楚all_questions是什么。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM