在一个数据库的item表中查询owner字段的值为mmxx的记录,会出现很多记录,他们的ID是自动编号,只有owner字段的值是相同的。现在要在查询出来的记录中任意选择一条,并对该条信息进行更新,同时返回该条记录的ID值。
比如: id    owner
       2     mmxx
       3     mmxx
       4     mmxx
     ...     mmxx
       10    mmxx

解决方案 »

  1.   

    这个需求比较神奇,比较简单的方法是
    select id from item where owner='mmxx' order by rand() limit 0,1
    这条语句可以将owner为mmxx的记录随机排序,并取出第一条
    然后用这个id来更新记录既可
      

  2.   

    如查mmxx是已知的测试下看看
    第一步:取出owner=id的所有记录,随机取一条
    $sql="select * from tablename where order owner='mmxx' by rand() limit 1"
    $result=mysql_query($sql);if($rows=mysql_fecth_array($result){
    $id=$ows['id'];
    }
    取出id第二步:如果有记录则$sql="update tablename set 字段='字段值',字段='字段值' where id='id值' "
      

  3.   

    mysql> select * from inventoryitems where order owner='bdmxd' by rand() limit 0,1;
    ERROR 1064 : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order owner='bdmxd' by rand() limit 0,1' at line 1..
    mysql> select * from inventoryitems where order owner='bdmxd' by rand() limit 1;
    ERROR 1064 : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order owner='bdmxd' by rand() limit 1' at line 1;
    大哥啊,怎么语法有错误啊?我用的是mysql5.0。希望指点下啊
      

  4.   

    帮我看看这个要怎么样改啊?55555555555555$sql="select * from inventoryitems where order owner='bdmxd' by rand() limit 1" 
    $result=mysql_query($sql);  if($rows=mysql_fecth_array($result){ 
    $id=$rows['inventoryitemid']; 


    $inventoryeitemsa = mysql_query("update inventoryitems set `characterid` = ".$name["id"]." and `itemid`=".$item["itemid"]." and `inventorytype`='1' and `position`=".$j." and `quantity`='1' and `owner`=".$name["name"]." where 'inventoryitemid`='$id' ",$connsql); $inventoryequipment = mysql_query("update inventoryequipment set `inventoryitemid` = '$id' and `upgradeslots` = ".$item["upgradeslots"]." and `level` = ".$item["level"]." and `str` = ".$item["str"]."' and `dex`=".$item["dex"]." and `int`=".$item["int"]." and `luk`=".$item["luk"]." and `hp`=".$item["hp"]." and `mp`=".$item["mp"]." and `watk`=".$item["watk"]." and `matk`=".$item["matk"]." and `wdef` = ".$item["wdef"]." and `mdef`=".$item["mdef"]." and `acc`=".$item["acc"]." and `avoid`=".$item["avoid"]." and `hands`=".$item["hands"]." and `speed`=".item$["speed"]." and `jump`=".$item["jump"]." and `owner`='mm'",$connsql);
      

  5.   

    select * from inventoryitems where owner='bdmxd' order by rand() limit 0,1
      

  6.   

    syntax error, unexpected T_VARIABLE .语法错误啊。提示错误的行数是  $result=mysql_query($sql); 
      

  7.   

    $inventoryequipment = mysql_query("update `inventoryequipment` set `inventoryitemid` = '$id' and `upgradeslots` = ".$item["upgradeslots"]." and `level` = ".$item["level"]." and `int`= ".$item["int"]." and `str` = ".$item["str"]." and `dex`=".$item["dex"]." and `int`=".$item["int"]." and `luk`=".$item["luk"]." and `hp`=".$item["hp"]." and `mp`=".$item["mp"]." and `watk`=".$item["watk"]." and `matk`=".$item["matk"]." and `wdef` = ".$item["wdef"]." and `mdef`=".$item["mdef"]." and `acc`=".$item["acc"]." and `avoid`=".$item["avoid"]." and `hands`=".$item["hands"]." and `speed`=".item$["speed"]." and `jump`=".$item["jump"]." and `owner`='mm' ",$connsql); 
    请问有没有什么语法错误。老是提示 $ 错误