[英]I have 2 tables and am trying to select at random a value that does not exist in the other table but I cant get it to work
The following is a questions table. 以下是一个问题表。 I am trying to select at random a question that has not already been answered and so does not exist in the
answered_questions
table. 我试图随机选择一个尚未回答的问题,因此在
answered_questions
表中不存在。 The following query includes values that already exist in questions_answered
以下查询包含
questions_answered
中已经存在的值
SELECT * FROM questions WHERE category = 'Random' and question NOT EXISTS (SELECT question FROM answered_questions WHERE game_id=6 and answered_questions.question = questions.question)ORDER BY Rand() LIMIT 10
Not exists donesn't compare with any field 不存在,请勿与任何字段进行比较
you may use WHERE NOT EXISTS (Subquery) 您可以使用WHERE NOT EXISTS(子查询)
That will be true if subquery has 0 rows or false if it has 1 or more rows. 如果子查询有0行,则为true;如果子查询具有1或更多行,则为false。
You can also use IN like WHERE question in (SELECT question from answers...) 您也可以在WHERE问题中使用IN(从答案中选择问题...)
That is true if the subquery returns the same question. 如果子查询返回相同的问题,则为真。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.