question table
========================
question_id
1
2
3
4
user_answer table
========================
user_id question_id
33 2
44 4
33 1
44 3
此代码将返回问题ID(2和1) 我想要的是从表格问题中检索其他问题ID,以便我想要 结果为(3和4)
$fadi = mysql_query("SELECT * FROM question
LEFT OUTER JOIN user_answer
ON user_answer.question_id = question.question_id
WHERE user_answer.user_id = 33");
Print "<table border cellpadding=3>";
while($info = mysql_fetch_array($fadi))
{ Print "<tr>";
Print "<th>question </th> <td>".$info['question_id'] . "</td></tr>";
} Print "</table>"; }
答案 0 :(得分:2)
我相信你要找的是IS NULL:
$fadi = mysql_query("SELECT * FROM question
LEFT OUTER JOIN user_answer
ON user_answer.question_id = question.question_id
AND user_answer.user_id = 33
WHERE user_answer.question_id IS NULL");
您只需从问题表中检索问题ID即可更进一步:
$fadi = mysql_query("SELECT question.question_id FROM question
LEFT OUTER JOIN user_answer
ON user_answer.question_id = question.question_id
AND user_answer.user_id = 33
WHERE user_answer.question_id IS NULL");
答案 1 :(得分:1)
编辑:改进版本。
$fadi = mysql_query("SELECT * FROM question WHERE question.question_id NOT IN (SELECT user_answer.question_id FROM user_answer WHERE user_answer.user_id = 33)");
Print "<table border cellpadding=3>";
while($info = mysql_fetch_array($fadi))
{ Print "<tr>";
Print "<th>question </th> <td>".$info['question_id'] . "</td></tr>";
} Print "</table>"; }
MySQLi版本:
$link = mysqli_connect($hostname, $username, $password, $database);
if (!$link){
echo('Unable to connect to database');
}
else{
$fadi = mysqli_query("SELECT * FROM question WHERE question.question_id NOT IN (SELECT user_answer.question_id FROM user_answer WHERE user_answer.user_id = 33)", $link);
Print "<table border cellpadding=3>";
while($info = mysqli_fetch_array($fadi,MYSQL_BOTH))
{ Print "<tr>";
Print "<th>question </th> <td>".$info['question_id'] . "</td></tr>";
} Print "</table>"; }
}
mysqli_close($link);
答案 2 :(得分:0)
可能的答案是采用您使用的相同mysql_query
然后从mysql_fetch_array
检索'question_id'值,最后,如果你有问题的计数(如果不是你可以使用mysql计数),使用php函数array_diff()
来检索(从增量排序) question_id的整数值数组或者来自'question'表中的question_id值数组的数组)正是你想要的