mysql - 给定特定条件随机查询SQL

标签 mysql

假设我有这个新闻收集数据库,其中 cat1、cat2 等是类别:

title     cat1  cat2  cat3  cat4  catcount  new1  new2  new3  new4
title1     1     2      0     0      2        0    0     0     0
title2     0     2      0     4      2        0    0     0     0
title3     1     2      3     0      3        0    0     0     0

现在我想随机选择标题及其任何非零类别,例如 title='title3' cat2='2' 进行一些处理以确定该类别是否正确。 然后将值返回给相应的new2(因为我们处理了cat2),递减catcount,并设置cat2 > 自处理后归零。

目标是处理标题类别的所有组合并将结果写入new,除非我随机读取它。 我如何在 MySQL 中查询这个?

编辑: 最终结果应该是这样的:

title     cat1  cat2  cat3  cat4  catcount  new1  new2  new3  new4
title1     0     0      0     0      0        1    0     0     0
title2     0     0      0     0      0        0    2     0     4
title3     0     0      0     0      0        0    2     0     0

最佳答案

您可以使用MySQL的RAND()函数生成伪随机数。

但是,请注意,此函数的熵非常低,因此您应该考虑获取表的所有条目,然后在代码中执行您单独解释的处理。

http://dev.mysql.com/doc/refman/5.7/en/mathematical-functions.html

RAND() is not meant to be a perfect random generator. It is a fast way to generate random numbers on demand that is portable between platforms for the same MySQL version.

关于mysql - 给定特定条件随机查询SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46787375/

相关文章:

php - 在php中每三个结果后显示横幅广告图像

mysql - mysql中倒序显示的记录

mysql 无法启动 Ubuntu 16.04.2 x64

mysql - 停止 mysql MEDIUMINT 在减法时最大化到 16777214

mysql - 如何在 native react 中显示 MySQL 数据库表的链接

PHP:一列表到一维数组

php - 如何在 mysql 表中自动链接上传

mysql - 我怎样才能让这个mysql查询更有效率呢?

mysql - MySQL中的case语句

mysql - 左连接为未连接的列提供了意外的结果