我知道这里有问题。任何人都可以指出它吗?我试图从问题中找出question_body,从答案中找出答案。
编辑:我收到此错误:字段列表中的列'question_id'不明确
<?php
$auctionSurvey = "SELECT question_id, survey_id, question_body, answer_body FROM questions
INNER JOIN answers ON answers.question_id = questions.question_id
WHERE survey_id='1'";
$aucResult = mysql_query($auctionSurvey) or die (mysql_error());
while($auctionRow = mysql_fetch_assoc($aucResult)){
echo $auctionRow['question_body']. $auctionRow['answer_body'];
}
?>
答案 0 :(得分:1)
看起来一个问题是模棱两可的专栏......
在SELECT列表中,我认为MySQL不能确定question_id
引用哪个表。
每当引用两个(或更多)表(行源)时,规范模式是对每个列引用进行QUALIFY ...
SELECT q.question_id
, q.survey_id
, q.question_body
, a.answer_body
FROM questions q
JOIN answers a
ON a.question_id = q.question_id
WHERE q.survey_id='1'
此模式有两大好处(符合条件的列引用):
首先,它可以帮助未来的读者“知道”哪些列来自哪个表,而不必查看表定义并找出哪个表具有哪个列名。
其次,当添加与另一个表中的列具有相同名称的新列时,它有助于避免“破坏”SQL语句...使列引用合格可避免破坏SQL语句的可能性(导致工作SQL语句开始引发模糊的列异常。
答案 1 :(得分:0)
您必须区分这两个表:
SELECT Q.question_id,
A.question_id,
Q.survey_id,
Q.question_body,
A.answer_body
FROM questions Q
INNER JOIN answers A ON A.question_id = Q.question_id
WHERE Q.survey_id='1'
答案 2 :(得分:0)
您的错误表明两个表中都有'question_id'。使用“别名”来区分表格,如下所示:
SELECT Q.question_id, survey_id, question_body, answer_body
FROM questions Q
INNER JOIN answers A ON A.question_id = Q.question_id
WHERE survey_id='1'";