Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 查找数据库中的最大数字并创建新变量_Php - Fatal编程技术网

Php 查找数据库中的最大数字并创建新变量

Php 查找数据库中的最大数字并创建新变量,php,Php,所以,我在数据库中有一个表,我有如下字符: A102 A897 B234 B23 C9823 C786 D345 etc... if ($variable==A) { $items = array(); foreach($value as $value) { $items[] = substr('$value', 1); max($items); // to find max } } 我需要实现的是: 如果用户输入了A,则函数应查找以A开

所以,我在数据库中有一个表,我有如下字符:

A102
A897
B234
B23
C9823
C786
D345 etc...
   if ($variable==A) {
    $items = array();
    foreach($value as $value) {
     $items[] = substr('$value', 1);
    max($items); // to find max
    }
   }
我需要实现的是: 如果用户输入了A,则函数应查找以A开头的所有变量 即 我有: A102和A897,我应该删除第一个字符,剩下102和897。 其中最大的是897,所以我应该创建一个新变量A897+1=A898,以此类推。 查询:

while( $row = mysql_fetch_assoc( $result)){
    $value[] = $row['id']; // Inside while loop
}
我想应该是这样的:

A102
A897
B234
B23
C9823
C786
D345 etc...
   if ($variable==A) {
    $items = array();
    foreach($value as $value) {
     $items[] = substr('$value', 1);
    max($items); // to find max
    }
   }
接近

$result = "SELECT * FROM formas WHERE 'id_f' LIKE '%A%'" ;  
$res = odbc_exec($connection, $result) or die(odbc_error());



    $biggest = 0;
    while( $row = odbc_fetch_array($res))
    {
         $current_value = substr($row['id_f'], 1); // return: 102, 897;
         if( $current_value > $biggest )
         {
            $biggest = $current_value; // in the last looping you should get 897.
         }
    }
    echo $result = "A(" . ($biggest + 1) . ")"; // return A(898)

我不确定是否传递参数。试试这个SQL

SELECT DER_NAME ||CHAR(COUNTER + 1)
FROM 
(
SELECT SUBSTR(NAME, 1) AS DER_NAME, MAX(INT(SUBSTR(2, NAME))) AS COUNTER
FROM TABLE-NAME
WHERE NAME LIKE 'A%' --SUBSTITUE YOUR PARAMETER
group by SUBSTR(NAME, 1)
) A

我想这个程序可以解决问题,如果我错了,请纠正我。 1.您应该创建一个从用户输入中获取第一个字符的函数。示例:ABC233,函数应返回一个2。一旦从步骤1中获得字符,就可以将其查询到表中。以下是完整的源代码:

> <?php  
> mysql_connect("localhost", "root","");
> mysql_select_db("stackoverflow");
> 
> 
> /* First chars */ 
> function first_char( $str ) 
> {
>     return substr($str, 0, 1); 
> } 
> ?>
> 
> <form action="home.php" method="post"> 
>      Input String : <input type="text" name="txtstring"><br> 
>      <input type="submit" value="Submit">
> </form>
> 
> <?php 
> if( $_POST['txtstring'] != '' ) 
> {    
>    $char = first_char($_POST['txtstring'] );    
>    $sql = mysql_query("SELECT * FROM mytablWHERE my_field LIKE '%" . $char . "%'") or die(mysql_error());
>  
>    
>     $biggest = 0;
>     while( $row = mysql_fetch_array($sql))
>     {
>          $current_value = substr($row['my_field'], 1); // return: 102, 897;
>          if( $current_value > $biggest )
>          {
>             $biggest = $current_value; // in the last looping you should get 897.
>          }
>     }
>     $result = "A(" . ($biggest + 1) . ")"; // return A(898)
>      
>     echo $result;
> 
> } ?>
我假设数据库名为stackoverflow,表名为mytable字段:ID,my_字段 结果是这样的:

A102
A897
B234
B23
C9823
C786
D345 etc...
   if ($variable==A) {
    $items = array();
    foreach($value as $value) {
     $items[] = substr('$value', 1);
    max($items); // to find max
    }
   }
让我知道它是否有效

(SELECT MAX(N) as maxid FROM (
SELECT CAST(SUBSTR(name, 2) AS UNSIGNED INTEGER) as N 
From `mytable` 
) t)

UNION ALL

(SELECT N as maxid FROM (
SELECT CAST(SUBSTR(name, 2) AS UNSIGNED INTEGER) as N 
From `mytable` 
ORDER BY N DESC) t LIMIT 1)

您只需使用一个查询:

SELECT MAX(N) as maxid FROM (
    SELECT CAST(SUBSTR(name, 2) AS UNSIGNED INTEGER) as N 
    From `mytable` 
    ) t


你有DB查询吗?还是那部分也有问题?是的,让我来写……如果你发现你必须处理A897中的897个子柱信息,你几乎肯定会出错。重新连接比拆分要有效得多。是的,重点是,我需要为A、B、C、D部分创建id,我需要A1、B1、C1、D1。。我不能用自动增量来做这件事。。这意味着有4个相同的值..这对我来说太复杂了:拜托,你能帮我简化一下吗,比如,举个例子。。因此,表名是formas,列名是id\f,我想存储第一个字符为a的最大数字,我在php中使用此查询。在使用like运算符的内部查询中,我们将查找从a开始的记录的最大数量。在外部查询中,您再次连接字母a和将1添加到最大值的结果来自内部查询的数字很抱歉打扰你,但是你能告诉我如何用php实现这个吗,我的意思是我需要回显你刚刚创建的新数字。。。我需要将它存储在一个值中!没关系。但我不懂php。我在这里给出的是一个查询,您可以使用它从数据库中检索。对不起,我没有帮助您好,是的,这个逻辑是正确的,但是当我回显结果时,我只得到值1。我很抱歉:我更改了$result=A+$maxist+1+;到$result=A$最大+1;再试一次!这是因为$maxist=0。没有找到任何记录。我再次更改了源代码,考虑这个“字段”,比如“%一%”。再试一次,它应该能工作-很抱歉,即使我修改了,它还是会打印1。让我粘贴代码,让你看看。汉克斯:这也很棒!所以,我的问题是,我需要找到所有包含A的名称。。如何保存变量中的值..只需在SQL server中将substr更改为SUBSTRING,并添加WHERE名称,如“A%”谢谢,它现在显示:SUBSTRING函数需要3个参数。很抱歉,我没有SQL server,您可以查看SQL server使用SUBSTRING的手册…我试图修改它。。你能告诉我我做错了什么吗?从中选择MAXN作为maxid,从formas中选择SUBSTRINGid\u f,2,lenid\u f-1作为N,其中id\f类似于“%C”t