Query the maximum value of a field and assign it to a variable, how to solve

Source: Internet
Author: User
Query the maximum value of a field and assign it to a variable
I see a lot of code on the Internet, but inserted into my file can not, please help me to see, how to query the maximum value of a field and then assign it to the variable, I am the query to add 1 calculation.



$db _host= "localhost";//server

$db _user= "root"; User name

$db _psw= "123456"; Password

$db _name= "Test";//Database name


$conn = mysql_connect ($db _host, $db _user, $db _psw) or Die ("link wrong");

mysql_select_db ($db _name, $conn) or Die ("link is wrong");

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

Every time you read a line in a CSV

if ($line <> 0)
{


$maxid + +;

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

$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 "Import succeeded";
?>

------Solution--------------------
SQL code
 #字段最大值: select MAX (ID) from table; 
------Solution--------------------
Explore

query maximum value resolves
$strSql = "Select M AX (order_no) from ' 0_sales_orders '; The

Table name Plus ' is OK, and I don't know why

and I want to please, I want to query 4 table ID maximum, how to write, write four code like that???

------Solution--------------------
discussion

How can I escape and give the code? Thank you!

------Solution--------------------
SQL code
select Max (c.id), Max (i.id)//c.id <==> t1.idfrom T1 as C,//definition Table T1 abbreviated as ct2 as I; As above, the abbreviation for defining table T2 is I 
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.