dsij89625 2013-04-19 04:08
浏览 126

根据特定条件在MySQL数据库中选择随机条目

Alright, let's say my MySQL table is set up with entries similar to:

id  code            sold
1   JKDA983J1KZMN49  0
2   JZMA093KANZB481  1
3   KZLMMA98309Z874  0

I'd like it to pick a random ID within the ranges already in the database (or just go from 1-X) and then just assign it to a variable for my own action to be taken. So let's say we want to pick a code that isn't sold (marked as 0 and not 1), then we'd pick it.

Now, it doesn't have to be 100% random, it could check if the first one is sold, if not, keep going. But I'm not 100% sure on how to go by this. Snippets would be appreciated because I can work out things easily on my own, I just need an example to see where I am going.

  • 写回答

5条回答 默认 最新

  • doutun1362 2013-04-19 04:10
    关注

    How about using a WHERE and ORDER BY RAND()

    SELECT id, code
    FROM tablename
    WHERE sold = 0
    ORDER BY RAND()
    LIMIT 1
    
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号