gpt4 book ai didi

PHP mySQL - 如何在使用 IN 后从行获取数据

转载 作者:行者123 更新时间:2023-11-29 20:26:59 25 4
gpt4 key购买 nike

我如何进行查询以访问 while 循环内的 answer_choice

在本例中我有两个表:

af_questions 
question_id

af_answers
answer_from // user id
answer_to // question id
answer_choice // user choice

PHP:

$questions = $db->prepare('SELECT * FROM af_questions WHERE question_id IN (SELECT answer_to FROM af_answers WHERE answer_from = :user_id)');
$questions->bindValue(':user_id', $user_id, PDO::PARAM_INT);
$questions->execute();

while ($row = $questions->fetch()) {
$question_id = $row['question_id'];
}

最佳答案

我不知道如何回答你提出的问题;这个问题对我来说没有意义。

在我看来,您真正想要的是这样的查询的结果:

  SELECT q.question.id
, a.answer_choice
FROM af_questions q
JOIN af_answers a
ON a.answer_to = q.question_id
WHERE a.answer_from = :user_id
ORDER BY q.question_id

然后在获取循环中,您将可以访问 question_idanswer_choice

<小时/>

我很确定这不能回答您提出的问题。但如果我们能够以此为后盾,我确信这种方法将帮助您实现目标。

关于PHP mySQL - 如何在使用 IN 后从行获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39259928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com