douzhang3898 2011-11-20 08:34
浏览 113
已采纳

插入数据库时​​,在1-1000之间的两个不同字段中插入一个数字,但不存在

Hi I need to be able to insert a random value between 1-1000 in to the "left" field and a random value between 1-1000 in the "top" field or a database row - however I want it to check if the "left" and "top" dont already have the 2 proposed values?

lets say left has been randmomly assigned the value 23 and top has been randomly assigned the value 967, if in the database it has already got an entry with left being 23 and top being 967 then randomly assign 2 new values that are not taken?

Cheers

  • 写回答

3条回答 默认 最新

  • doujia9204 2011-11-20 08:50
    关注

    This is pseudocode, but it shows the sequence of events:

    $exists = 1;
    
    do
    {
      // GENERATE $left and $top
    
      $query_results = /* SELECT * FROM table WHERE left = $left and top = $top */
    
      $exists = /* number of rows on $query_results */;
    
      if ($exist == 0)
      {
        // INSERT $left, $top INTO table
      }
    
    }
    while ($exists > 0); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度