Home > Backend Development > PHP Tutorial > 查询某个字段最大值,然后赋予给变量,该怎么解决

查询某个字段最大值,然后赋予给变量,该怎么解决

WBOY
Release: 2016-06-13 10:23:28
Original
1024 people have browsed it

查询某个字段最大值,然后赋予给变量
我在网上看到好多代码,可是插入到我的文件里面就不行,请各位帮我看看,怎么样查询某字段最大值然后把它赋予变量,我是查询到之后进行加1计算。



$db_host="localhost";//服务器  

$db_user="root"; //用户名  

$db_psw="123456"; //密码  

$db_name="test";//数据库名  
 
 
$conn = mysql_connect($db_host,$db_user,$db_psw) or die("链接错啦");  

mysql_select_db($db_name,$conn) or die("链接又错啦");  

mysql_query("set names utf-8");  


$line = 0;
$file = fopen('upload/amazon_erp.csv',"r"); 


$strSql="SELECT MAX(sku) FROM table";

$query = mysql_query($strSql,$conn);
$resulta= mysql_fetch_row($query);
$maxid=$resulta;

print_r($resulta);

  while ($result = fgetcsv($file)) {  
  
  //每次读取CSV里面的一行内容  

if ($line 0)
  {


 $maxid++;

  $sku=$result[0];
  $name=$result[1];
  $address=$result[2];
  $a=$result[3];
  $b1=$result[16];
  $b2=$result[17];
  $b3=$result[18];
  $b="$b1,$b2,$b3";

  $c="$sku,$name,$address,$a,$b";

echo $c;
 echo "
";
 
 $sql="INSERT INTO `test`.`table` (`$resulta`, `name`, `address`, `a`, `b`) VALUES ('$resulta','$name','$address','$a','$b')";

  mysql_query($sql); 
 } 
  $line++;
 
 fclose($file);  
echo "导入成功";
?>

------解决方案--------------------

SQL code
#字段最大值:select MAX(id) from table;<br><font color="#e78608">------解决方案--------------------</font><br>
Copy after login
探讨

查询最大值的问题解决了
$strSql="SELECT MAX(order_no) FROM `0_sales_orders`";

表名加''就是行,也不知道为什么

还有我想请,我一次要查询4个表的ID最大值,应该怎么写,写四段那样的代码????

------解决方案--------------------
探讨

请问怎么转义,能给出代码吗?谢谢!

------解决方案--------------------
SQL code
SELECT MAX(c.id),MAX(i.id)  //c.id  t1.idFROM t1 AS c,               //定义表t1的缩写为ct2 AS i;                    //如上,定义表t2的缩写为i<div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template