[英]MySQL - SELECT / JOIN from two tables - inverted or negative
i want to make survey on my site and i want to every survey question is displayed to user (registered and logged in) only once (if answered). 我想在我的网站上进行调查,并且我希望每个调查问题仅向用户(注册和登录)显示一次(如果回答)。 So i made 2 tables.
所以我做了2张桌子。
surveyQuestions
(table 1 with data for each questions): surveyQuestions
(表1中包含每个问题的数据):
surveyAnswers
(table 2 with answers of each user to questions): surveyAnswers
(表2,每个用户对问题的回答):
What i need is to select 1 question from table surveyQuestions
which has not been yet answered from logged user (answers are stored in surveyAnswers
). 我需要从表
surveyQuestions
选择1个问题,该问题尚未得到登录用户的答复(答案存储在surveyAnswers
)。 UserId
in browser is handled using $_SESSION['id']
. 浏览器中的
UserId
使用$_SESSION['id']
。
I have tried different JOIN
methods, but without luck and i am lost now. 我尝试了不同的
JOIN
方法,但是没有运气,我现在迷路了。
Thank you very much. 非常感谢你。
assuming you have distinct questions in your questions table and multiple duplicates of each question for different users in your answers table you should be able to do it like this.. get survey id's for all answered questions for a particular user and then look at the questions they havent answered by using NOT IN 假设您的问题表中有不同的问题,而答案表中的不同用户对每个问题有多个重复,则您应该可以这样做。.获取特定用户所有已回答问题的调查ID,然后查看问题他们还没有使用NOT IN来回答
SELECT whatever_you_want
FROM surveyQuestions
WHERE surveyId NOT IN(
SELECT surveyId
FROM surveyAnswers
WHERE userId = $id -- # -- whatever your filtering id is here
)
LIMIT 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.