简体   繁体   English

MySQL:选择第n行,其中至少有一行具有属性集?

[英]MySQL: Select nth rows where at least one has a attribute set?

I've a table in MySQL called words . 我在MySQL中有一个名为words的表。

One column is called word (the actually word) and the other is called special 一列称为word (实际上是单词),另一列称为special

I've a query I'm trying to perform which - if possible - should do this: 我有一个我正在尝试执行的查询 - 如果可能的话 - 应该这样做:

SELECT word FROM words WHERE special = 1 AT LEAST ONCE

This is of cause a pseudo-like query but the thing I want is to get randomly nth records from my words table where AT LEAST ONE of the words has the attribute special set to 1. 这是一个伪类查询,但我想要的是从我的单词表中随机获得第n条记录,其中至少有一个单词的属性特殊设置为1。

It means that all nth records is allowed to be 1 if this is the case or all but one of the nth records can have special = 0 but there have to be at least one which has the special = 1. 这意味着如果是这种情况,则允许所有第n个记录为1,或者除了第n个记录中的一个之外的所有记录都可以具有特殊 = 0但是必须至少有一个具有特殊 = 1的记录。

I've tried something like: SELECT * FROM words HAVING COUNT(isNum = 1) > 1 ORDER BY RAND() LIMIT 10 or something like this.. It does not give me the result I want back, actually it only returns one result. 我尝试过类似的东西: SELECT * FROM words HAVING COUNT(isNum = 1) > 1 ORDER BY RAND() LIMIT 10或类似的东西..它没有给我想要的结果,实际上它只返回一个结果。

Can this be done by SQL?? 可以通过SQL完成吗?

Thanks 谢谢

Here is an approach that should be relatively efficient, sort of. 这是一种应该相对有效的方法。 It selects one random "special" word, then n + 1 other words (which might contain the first word). 它选择一个随机的“特殊”字,然后是n + 1个其他字(可能包含第一个字)。 It orders them so the random one is guaranteed to be first, and then select n of them. 它命令它们保证随机的第一个,然后选择它们中的n个。

select word
from ((select word, 0 as ordering
       from words
       where special = 1
       order by rand()
       limit 1
      ) union all
      (select word, rand() as ordering
       from words
       limit n
      )
     ) t
group by word
order by min(ordering)
limit n;
SELECT *
FROM words
HAVING COUNT(special = 1) > 1
ORDER BY RAND()
LIMIT 10

Using COUNT() here without GROUP simply counts the number of records and returns 1 row, then applies the HAVING clause to that one row, yielding one record in your result set. 在没有GROUP情况下使用COUNT()只计算记录数并返回1行,然后将HAVING子句应用于该行,从而在结果集中生成一条记录。

If you add COUNT() to the SELECT clause, you'll kind of see your mistake: 如果你将COUNT()添加到SELECT子句,你会发现你的错误:

SELECT COUNT(special = 1), words.*
FROM words
HAVING COUNT(special = 1) > 1
ORDER BY RAND()
LIMIT 10

The record that it returns after the count is indeterminate. 计数后返回的记录是不确定的。

So, you must add GROUP BY to get multiple records back (one per word): 因此,您必须添加GROUP BY以获取多个记录(每个单词一个):

SELECT *
FROM words
GROUP BY word
HAVING COUNT(special = 1) > 1
ORDER BY RAND()
LIMIT 10

Now, you'll notice that it returns all words with more than one record, regardless of whether it has special = 1 . 现在,您将注意到它返回具有多个记录的所有单词,无论它是否具有special = 1

That's because special = 1 is a Boolean expression that returns 0 or 1 . 那是因为special = 1是一个返回01的布尔表达式。 COUNT() increments on both COUNT(0) and COUNT(1) . COUNT()COUNT(0)COUNT(1)上递增。 Actually, it increments on anything but COUNT(NULL) . 实际上,除了COUNT(NULL) ,它会增加任何COUNT(NULL) Now, you realize that you really want SUM() . 现在,你意识到你真的想要SUM()

SELECT *
FROM words
GROUP BY word
HAVING SUM(special = 1) > 1
ORDER BY RAND()
LIMIT 10

Alternatively, and perhaps more straight-forward: 或者,也许更直接:

SELECT DISTINCT word
FROM words
WHERE special = 1
ORDER BY RAND()
LIMIT 10

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

相关问题 MySQL: Select 第 n 行至少有 m 属性? - MySQL: Select nth rows where at least have m attribute? MySQL:创建至少一个属性所属的所有行 - MySQL: Create all the rows where at least one attribute belongs MySQL-如何选择主键多次出现并且至少另一个字段匹配的行? - MySQL - how to select rows where a primary key occurs more than once AND at least one of another field matches? MYSQL选择3条随机记录,其中至少一个记录字段具有特定值 - MYSQL select 3 random records where at least one record-field has certain value SQL选择行,其中每个ID至少有一个值 - SQL select rows where at least one value is something per id MySQL选择id,其中至少有一个值不在列表中 - MySQL select id where at least one value not in list 如何选择组及其所有用户,该组中至少有一个用户在最近的X天内访问了MySQL - How do you select groups and all of their users where at least one user in the group has visited within the last X days in MySQL MySQL查询选择行在整个结果集中具有两个值的行 - MySQL Query To Select Rows Where Column Has Both of 2 Values in Entire Result Set SQL select 具有相同代码的所有行,其中至少一行包含特定值 - SQL select all rows with the same ticker where at least one of the rows includes a specific value MySQL-选择至少一个或不选择 - Mysql - Select at least one or select none
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM