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条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵