简体   繁体   中英

Querying database from result of other query

I need help building a MySQL query to query 3 tables.

I am building an online quiz with stored answers in MySQL and PHP. I am using 3 tables for the user, questions and answers. I want to provide the users with questions that they've not answered previously but can't get the query to work.

The database table structures are as follows:-

Users

user_id
language
+ other things

questions

id
question_number
text
language

answers

answer_value
id
question_number
user_id

I can obtain the answers that they have completed by using

SELECT questions.question_number, questions.text 
from questions 
JOIN answers ON answers.question_number=questions.question_number
JOIN users on users.user_id=answers.user_id 
WHERE questions.language=users.language AND users.user_id='1'

This returns the text for the questions answered but I want to obtain the question number and text for the questions that they haven't answered. I think I've pulled all my hair out trying to solve this one. Any help greatly appreciated.

I've solved it thanks by using

SELECT DISTINCT questions.question_number, questions.text FROM questions WHERE questions.question_number NOT IN(SELECT answers.question_number FROM answers WHERE answers.user_id=$var)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM