如何准备查询以从另一个表检查,然后从另一个表中获取

时间:2016-04-24 10:17:01

标签: php mysql

我有两个表一个是random_questions,另一个是personal_questions,我想检查个人事项中的copy_id列personal_questions.question_to = current_session_userpersonal_question.copy_id = random_question.id ....

这是我的代码:

$session_user = $_SESSION['user_id'];

$sql = mysqli_query($con, "SELECT DISTINCT random_questions.id AS id, random_questions.question_by_id AS question_by_id, random_questions.question AS question, random_questions.total_answers AS answers FROM random_questions INNER JOIN personal_questions WHERE (random_questions.total_answers != '8') OR (personal_questions.question_to != '$session_user' AND personal_questions.copy_id != random_questions.id)");

我没有正确得到结果,如果personal_questions的copy_id等于任何random_questions的id,则结果会自动重复。我希望你明白。请帮忙。

1 个答案:

答案 0 :(得分:0)

Select *
from   random_questions   p,
       personal_questions a
where  a.question_to = current_session_user
and    a.copy_id     = p.id;