希望列出所有问题以及个人的所有答案。
我不知道我应该使用还是内部选择。
Tbquestions
id - 成员 - 问题 - 标题
Tbanswers
id - 成员 - 回答 - 标题
只需要获得每个表的标题
答案 0 :(得分:3)
尝试此查询:
SELECT id,member,'Q' as Type, question as QA FROM Tbquestions
WHERE member='member1'
UNION ALL
SELECT id,member,'A' as Type, answer as QA FROM Tbanswers
WHERE member='member1'
示例输出:
id member Type QA
1 Name1 Q What is it?
3 Name4 Q Who are you?
1 Name2 A My name is Name2
4 Name3 A He is dead, Jim.
修改强>
为每个查询添加Limit
子句,以查找5个问题和10个答案:
SELECT id,member,'Q' as Type, question as QA FROM Tbquestions
WHERE member='member1' LIMIT 5
UNION ALL
SELECT id,member,'A' as Type, answer as QA FROM Tbanswers
WHERE member='member1' LIMIT 10
答案 1 :(得分:0)
如果问题与相应答案之间存在关联,则应该加入。 由于表格与会员ID相关,我不认为应该使用联接。而是使用UNION ALL
简单地选择并组合来自两个表的结果集答案 2 :(得分:0)
global $mysqli;
$stmt = $mysqli->prepare("SELECT title from Tbquestions WHERE member = ?");
$stmt->bind_param('s',$member);
if($stmt->execute()){
$stmt->bind_result($questions);
$stmt->close();
}
$stmt = $mysqli->prepare("SELECT title from Tbanswers WHERE member = ?");
$stmt->bind_param('s',$member);
if($stmt->execute()){
$stmt->bind_result($answers);
$stmt->close();
}
$allposts = array_merge($questions, $answers);
echo '<pre>';
print_r($allposts, false);