简体   繁体   English

SQL:从表中选择随机行,其中该行的ID不在另一个表中?

[英]SQL: select random row from table where the ID of the row isn't in another table?

I've been looking at fast ways to select a random row from a table and have found the following site: http://74.125.77.132/search?q=cache:http://jan.kneschke.de/projects/mysql/order-by-rand/&hl=en&strip=1 我一直在寻找从表中选择随机行的快速方法,并找到以下站点:http: //74.125.77.132/search?q=cache :http: //jan.kneschke.de/projects/mysql / order-by-rand /&hl = zh-CN&strip = 1

What I want to do is to select a random url from my table 'urls' that I DON'T have in my other table 'urlinfo'.The query I am using now selects a random url from 'urls' but I need it modified to only return a random url that is NOT in the 'urlinfo' table. 我想做的是从我的其他表'urlinfo'中没有的表'urls'中选择一个随机URL。我正在使用的查询现在从'urls'中选择一个随机URL,但我需要对其进行修改只返回不在“ urlinfo”表中的随机网址。

Heres the query: 这是查询:

SELECT url 
FROM urls JOIN (SELECT CEIL(RAND() * (SELECT MAX(urlid)
                                     FROM urls
                                     )
                           ) AS urlid 
               ) AS r2 USING(urlid);

And the two tables: 和两个表:

CREATE TABLE urls (
 urlid INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
 url VARCHAR(255) NOT NULL
) ENGINE=INNODB;


CREATE TABLE urlinfo (
 urlid  INT NOT NULL PRIMARY KEY,
 urlinfo VARCHAR(10000),
 FOREIGN KEY (urlid) REFERENCES urls (urlid)
   ) ENGINE=INNODB;

How about working from this random solution: 如何使用此随机解决方案:

SELECT TOP 1 * FROM urls
WHERE (SELECT COUNT(*) FROM urlinfo WHERE urlid = urls.urlid) = 0
 ORDER BY NEWID()

You need to first do a left outer join to get the set of records in 'urls' that are not in 'urlinfo', then pick a random record from that set. 您需要首先执行左外部联接,以获取不在“ urlinfo”中的“ url”中的记录集,然后从该记录集中选择一个随机记录。

SELECT * FROM urls
LEFT OUTER JOIN urlinfo
ON urls.urlid = urlinfo.urlid
WHERE urlinfo.urlid IS null

Now pick a random row from this set - you can do something like 现在从该集合中随机选择一行-您可以执行类似

SELECT newUrls.url
FROM (    
      SELECT urls.urlid, urls.url FROM urls
      LEFT OUTER JOIN urlinfo
      ON urls.urlid = urlinfo.urlid
      WHERE urlinfo.urlid IS null
     ) as newUrls
WHERE urls.urlid >= RAND() * (SELECT MAX(urlid) FROM urls) LIMIT 1

However, this will only work if the urlids in urlinfo are roughly randomly distributed across the range of possible values. 但是,只有在urlinfo中的urlid大致随机分布在可能值的范围内时,这才起作用。

You could use where not exists to exclude rows that are in the other table. 您可以使用where not exists来排除另一个表中的行。 For a random row, one option is a order by rand() with a limit 1 : 对于随机行,一个选项是order by rand()limit 1order by rand()

SELECT url
FROM urls
WHERE NOT EXISTS (
    SELECT *
    FROM urlinfo ui
    WHERE ui.urlid = urls.urlid
)
ORDER BY RAND()
LIMIT 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL将内容从1个表移动到另一个基于行ID的位置 - SQL move content from 1 table to another based where row id 从表中选择行,其中具有相同id的另一个表中的行在另一列中具有特定值 - Select rows from a table where row in another table with same id has a particular value in another column 如何基于另一个表中的id选择表中的最新行 - how to select latest row in a table based on id from another table SQL为其他表中的每个ID选择不同的位置 - SQL select distinct where exist row for each id in other table SQL Select where 两个值不在另一个表的同一行中 - SQL Select where two values not in the same row of another table SQL select:一个表中的随机顺序值,用于另一个表中的每一行 - SQL select: random order values from one table for each row in another sql查询根据之前的查询从表中随机选择一行 - sql query to select a random row from a table based on the previous query 更新来自另一个表SQL Server 2014的随机行 - UPDATE random row from another table SQL Server 2014 SQL如果在一个表中不存在,则从另一表中选择/插入行 - SQL Select/insert a row from another table if it doesn't exist in one table 查询以使用ID从另一个表中选择一行 - Query to select a row from a table from another using id
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM