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 onvif框架引用一直报错
  • ¥15 PHP关于输出文本的格式变更问题
  • ¥50 C#和C++混合编程,使用CLR托管,报错System.Runtime.InteropServices.SEHException
  • ¥30 .NET使用sqlite发布后报错
  • ¥15 微信小程序获取华为云影子设备失败,显示403报错
  • ¥15 Unity在WebGL平台导出Word报错问题
  • ¥15 ghpython这里总是报错而且rhino视图窗口内不显示怎么办
  • ¥15 布尔运算推导,请证明
  • ¥15 有没有人会打学生成绩管理系统呀
  • ¥15 在使用Fiddler和夜神模拟器抓包的时候一直出现443该怎么办啊QAQ搜了好几个笔记都没有解决