Home > Database > Mysql Tutorial > 使用SQL语句从数据库一个表中随机获取数据_MySQL

使用SQL语句从数据库一个表中随机获取数据_MySQL

PHP中文网
Release: 2016-05-27 14:29:38
Original
1597 people have browsed it

-- 随机获取 10 条数据

SQL Server:SELECT TOP 10 * FROM T_USER ORDER BY NEWID()
ORACLE:SELECT * FROM (SELECT * FROM T_USER ORDER BY DBMS_RANDOM.RANDOM()) WHERE RONUM <= 10
MySQL:SELECT * FROM T_USER  ORDER BY  RAND() LIMIT 10
Access:SELECT TOP 10 * FROM T_USER ORDER BY rnd([一个自动编号字段])
Copy after login

这条语句在 Access 中的“查询”中是可以运行并得到随机结果的,但在 ASP.NET等后台程序代码中却无法得到预期的随机效果。
正确的写法如下:

以ASP.NET为例:

Random random = new Random(System.Guid.NewGuid().GetHashCode());
int r = random.Next();
string sql = "SELECT TOP 10 * FROM T_USER ORDER BY RND(" + (-r) + "*自动编号字段)"
Copy after login

以上就是使用SQL语句从数据库一个表中随机获取数据_MySQL的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template