[英]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
是一个返回0
或1
的布尔表达式。 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.