获取随机代码不存在mysql
我有这个 sql
select concat( char(FLOOR(97+ RAND()*26))
, char(FLOOR(97+ RAND()*26))
, FLOOR(100+ RAND()*999)
, char(FLOOR(97+ RAND()*26))) AS randomcode
WHERE NOT EXISTS (
SELECT *
FROM table
WHERE code_felt = randomcode );
但我无法让它工作。有人知道我在这里做错了什么吗?
我需要随机制作一个 6 图表代码,但它在我的代码中不存在。我希望我能得到帮助。
I have this sql
select concat( char(FLOOR(97+ RAND()*26))
, char(FLOOR(97+ RAND()*26))
, FLOOR(100+ RAND()*999)
, char(FLOOR(97+ RAND()*26))) AS randomcode
WHERE NOT EXISTS (
SELECT *
FROM table
WHERE code_felt = randomcode );
But I can't get it to work. Does somebody know what I am doing wrong here?
I need to make a 6 chart code in random and it does not exists in my code. I hope I can be helped.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你必须从某个地方选择。您的 SELECT 没有表名。由于您实际上并未从表中选择随机值,因此您应该使用 DUAL (这是一个“假”表):
这有时会选择一条记录,有时不会选择任何记录。如果它没有选择任何记录,则必须重复查询,我想不出一种方法让它始终选择一条记录。
You have to select FROM somewhere. Your SELECT does not have a table name. Since you don't actually select random values from a table, you should use DUAL (this is a 'fake' table):
This will sometimes select one record, sometimes no records. If it selects no records, you have to repeat the query, I can't think of a way to make it always select one record.
试试这个:
Try this: