简体   繁体   English

我有2个表,并且试图随机选择另一个表中不存在的值,但我无法使其正常工作

[英]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

ANSWERED TABLE 回答表

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中已经存在的值

QUESTIONS TABLE 问题表

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.

相关问题 尝试使用 Flask SqlAlchemy event.listen 在我的表中更改自动增量起始值。但我遇到表不存在 - Trying to change auto increment start value in my tables with Flask SqlAlchemy event.listen.But I am encountering table doesn't exist 我正在尝试加入其中一个表中不存在用户记录的表 - I am trying to join tables where a user record does not exist in one of the tables 能行吗? 用select插入...我有我自己的价值,想从其他表中获取一些数据 - can this work? insert into with select…i have my own value and want to take some data from other table 我正在尝试使用相同的INNER JOIN函数中的2个,但是我无法使其正常工作 - I am trying to use 2 of the same INNER JOIN function but i cant get it to work 我无法让我的连接在某些表上工作,但它们会在其他表上工作。 试图从 2 个表中选择列 - I cannot get my joins to work on a some tables, but they will work on others. Trying to select columns from 2 tables 我正在尝试从2个mysql表中选择数据到1个查询中 - I am trying to select data from 2 mysql tables into 1 query 插入多个数据,这些数据具有select语句,并且该select语句的FROM表与我要插入的表相同 - Insert multiple data, which have select statement and the FROM table of that select statement is same as the table I am trying to insert 如果我的表与其他表没有关系,这是否正常? - Is it normal if I have table with have no relationship with other tables? QUERY DATABASE:如何从 2 个表中获取多个数据,其 id 不存在于表中 - QUERY DATABASE: How can I get multiple data from 2 tables with an id that does not exist in a table 我正在尝试从不同的表中导出数据。 我从表中拉了相同的标题 - I am trying to export data from different tables. I have pulled same headers from the tables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM