我正在创建PHP的在线讨论论坛作为我的上一学期项目,我想列出所有在未答复页面中没有答案的问题,与Stack Overflow中的相同。
但我不知道如何实现它;我在互联网上搜索了很多但仍无法找到如何列出所有没有答案的问题。
以下是我的代码:
<?php
@session_start();
include("include/user_header.php");
include("include/connection.php");
$query="SELECT * FROM question WHERE NOT EXISTS (SELECT * FROM answer WHERE answer.question_id = question.question_id);"
$result = mysql_query($query) or die(mysql_error());
?>
<script type="text/javascript">
if(action=="pass_id")
{
document.frm_cms.action="answer.php";
<!--document.frm_que.myaction.value=action;-->
document.frm_cms.id.value=id;
document.frm_cms.submit();
}
</script>
<div class="main-container">
<div class="another-main-container row">
<div class="ask-question-container">
<div class="ask-question-container-inner row">
<form name="frm_cms" method="post" class="printable">
<div class="question-usr-prfl row">
<h1 class="usr-hdng"><span class="qustn-fgr-usr-prfl"></span> Questions</h1>
<ul class="row list-of-questions">
<?php
while($row = mysql_fetch_array($result))
{
?>
<li class="row">
<a href="javascript:setAction('pass_id','<?php echo $row['question_id']; ?>');" >
<?php echo $row['title']; ?>
</a>
<div class="quest-tag-cntr row">
<?php
$tgNm = explode(",",$row['tag_name']);
for($k=0;$k<count($tgNm);$k++)
{
$k+1;
?>
<a href='question.php?id=<?php echo $tgNm[$k]; ?>'>
<?php
echo getFromId("tag", "tag_name", "id", $tgNm[$k]);
?>
</a>
<?php
}
?>
</div><!--/quest-tag-cntr-->
</li>
<?php
}
?>
</ul>
</div><!--/question-usr-prfl-->
</form>
</div>
</div>
</div>
</div>
<?php
include("include/user_footer.php");
?>
答案 0 :(得分:1)
SELECT *
FROM question
WHERE NOT EXISTS (
SELECT *
FROM answer
WHERE answer.question_id = question.question_id
);
答案 1 :(得分:1)
我觉得这样的事情值得一试:
SELECT DISTINCT question.id
FROM question
LEFT JOIN answer ON (question.id = answer.question_id)
WHERE answer.id IS NULL
我已离开加入答案表(因为我们希望该连接失败),然后将非空列与NULL
进行比较。因此,返回的行是没有答案的问题。