-1

I want to fetch and display a limited amount of questions that hasn't been answered yet by the logged in user only.

I already have this block of code, but I can't seem to find out how to select it distinctly for a certain logged in user.

SELECT
   a.* 
FROM
   questions as a 
   LEFT JOIN
      questions_answers as b 
      ON (a.q_id = b.q_id) 
WHERE
   b.q_id IS NULL 
   AND b.user_id = 2 
ORDER BY
   RAND() LIMIT 10

assuming "2" is a sample user ID of the logged in user.

The "AND b.user_id = 2" is where I'm stuck.

I'll appreciate any help.


UPDATE: Here's the table structure

Questions Table
q_id
question

Questions_Answers Table
ans_id
q_id
user_id

UPDATE: I have already answered this question and added it below. Thank you to everyone who suggested. I'll take your advice seriously as well. I'll appreciate it if you will upvote my answer if you have tried it and get what I meant. :)

jaegyo23
  • 23
  • 1
  • 12

4 Answers4

0

I am little bit confused about your tables structure however i tried to answer it assuming you are storing answers and answering users_id in answers table -

Update: Thanks for providing table structure please see below update query.

SELECT questions.*, questions_answers.* 
FROM questions 
LEFT JOIN questions_answers ON questions.q_id = questions_answers.q_id 
WHERE questions_answers.user_id <> '2'

OR

SELECT questions.*, questions_answers.* 
FROM questions 
LEFT JOIN questions_answers ON questions.q_id = questions_answers.q_id 
WHERE NOT(questions_answers.user_id = '2')
Muhammad Tarique
  • 1,407
  • 1
  • 13
  • 17
0

You can use left join with

SELECT a.*
FROM questions as a 
 LEFT JOIN questions_answers as b ON (a.q_id = b.q_id)
WHERE  b.q_id IS NULL 
AND b.user_id = 2 

Please check below link How to select all records from one table that do not exist in another table?

Justin J
  • 808
  • 8
  • 14
  • I am able to get the answer using a slight change on the block of code I did. I will upload the answer I did on my own so that the readers will be able to know the correct one. Thank you for your answer – jaegyo23 Nov 29 '18 at 06:56
0

And so, after a few trials (thanks to everyone who posted their suggestions), I was able to manage to get the data I am wanting for using the following block of code. :)

SELECT
   a.* 
FROM
   questions as a 
LEFT JOIN
   questions_answers as b 
   ON (a.q_id = b.q_id) AND b.user_id = 2
WHERE
   b.q_id IS NULL 
ORDER BY RAND() 
LIMIT 10

Sharing this so everyone who stumbles down on my post will get the correct answer. I'll appreciate if you will upvote this one if you have tried the code and got the answer. :)

jaegyo23
  • 23
  • 1
  • 12
0
SELECT
   a.* 
FROM
   questions as a 
WHERE
  a.q_id not in(select q_id from questions_answers where user_id = 2) 
ORDER BY
   RAND() LIMIT 10

You can achieve this by simple inner query. I hope this will help you.

sqlfiddle : http://sqlfiddle.com/#!9/fee4ba/8

Or

SELECT * FROM (SELECT
   a.* ,b.ans_id
FROM
   questions as a 
   left JOIN
      questions_answers as b 
      ON (a.q_id = b.q_id) AND b.user_id = 2 ) as c
WHERE c.ans_id IS null
ORDER BY
   RAND() LIMIT 10
Sumesh TG
  • 2,557
  • 2
  • 15
  • 29
  • I posted my answer which got the data I needed. :) Thank you for the effort and as much as I want to upvote your answer because it worked for me too, I can't but just letting people know your answer worked as well. :) – jaegyo23 Nov 29 '18 at 07:19
  • @jaegyo23 do want to achieve this result using only join query? – Sumesh TG Nov 29 '18 at 07:21
  • I'm not thinking of limiting it to only join query.. does that affect the data being retrieved? – jaegyo23 Nov 29 '18 at 07:26