Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带条件的mysql随机_Mysql - Fatal编程技术网

带条件的mysql随机

带条件的mysql随机,mysql,Mysql,我在mysql中有一个表,比如说table1 我在上面运行这个: SELECT FLOOR( MAX(id) * RAND()) FROM `table1` 这很有效,但我现在尝试添加一个条件“AND tom” 其中tom是一个整数字段 例如: id tom 1 0 2 3 3 2 4 0 5 0 6 3 7 1 8 1 9 3 等等 所以我的问题是, 我如何从id中选择一个随机值,它也满足tom='0'的要求 SELECT id FROM `table1

我在mysql中有一个表,比如说table1

我在上面运行这个:

SELECT FLOOR( MAX(id) * RAND()) FROM `table1`
这很有效,但我现在尝试添加一个条件“AND tom”

其中tom是一个整数字段

例如:

id tom
1   0
2   3
3   2
4   0
5   0
6   3
7   1
8   1
9   3
等等

所以我的问题是,

我如何从id中选择一个随机值,它也满足tom='0'的要求

SELECT id FROM `table1` WHERE tom = 0 ORDER BY RAND() LIMIT 1
这将首先获取
tom=0
的所有行,然后对这些结果进行排序。MySQL然后将这些结果限制为一个,返回您想要检索的单个值

select * from (
select * from table where tom = 0 ) as t order by rand() limit 1

这将首先获取
tom=0
的所有行,然后对这些结果进行排序。MySQL会将这些结果限制为一个,返回您想要检索的单个值。

我希望我理解正确:

select * from (
select * from table where tom = 0 ) as t order by rand() limit 1
SELECT id FROM `table1` WHERE tom = 0 order by rand() limit 1

我希望我理解正确:

SELECT id FROM `table1` WHERE tom = 0 order by rand() limit 1

为什么按“RAND()limit 1”排序?它只返回一个值,没有它,对吗?没有它,它将以随机顺序返回tom=0的所有值。带限制的将返回第一个。为什么按“RAND()limit 1”排序?它只返回一个值,没有它,对吗?没有它,它将以随机顺序返回tom=0的所有值。带限制的将返回第一个。