简体   繁体   English

获取一个随机数,并检查mysql id是否存在

[英]get a random number and check if mysql id exists

I want to generate a random number and search for the entry by id in a mysql database I select and if this id does not exist in the database, search for a new one, as long as the script finds one.Then output some line form this row. 我想生成一个随机数,然后在我选择的mysql数据库中按id搜索条目,如果该id在数据库中不存在,则只要脚本找到一个,就搜索一个新的id。然后以某种行格式输出此行。 sounds easy but don't know how to handle the errors. 听起来很简单,但不知道如何处理错误。

found this one: 找到了这个:

    SELECT name, address, ROUND(100.0 + 400.0 * RAND()) AS random_number
    FROM users

but here isn't the error handler included. 但是这里没有包含错误处理程序。 Can you help here? 你能帮忙吗?

If you want one random record you can use ORDER BY RAND() LIMIT 1 如果您想要一个随机记录,则可以使用ORDER BY RAND() LIMIT 1

SELECT name, address FROM users ORDER BY RAND() LIMIT 1

This will give you one random user. 这将给您一个随机用户。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM