简体   繁体   English

如何从包含字母数字字符串的列中获取最大值?

[英]How to get the max value from a column with alphanumeric strings?

I have a table which contains invoice and estimate numbers.The invoice numbers are like "IN1000","IN1001","IN1002" and so on.The estimate numbers are like "ES101","ES102","ES103" .How can I get the max of both my invoice and estimate?我有一张包含发票和估算编号的表格。发票编号如"IN1000","IN1001","IN1002"等。估算编号如"ES101","ES102","ES103"如何我得到我的发票和估价的最大值? I would also like it to be cast into an integer like 1000.我也希望它像 1000 一样被转换成 integer。

I have tried the following query:我尝试了以下查询:

SELECT Max(CAST (SUBSTR(invoiceNo,3) AS UNSIGNED)) FROM selected_items 
WHERE invoiceNo RLIKE 'IN';

When I run this query I get the following error:当我运行此查询时,出现以下错误:

#1064 - You have an error in your SQL syntax; #1064 - 您的 SQL 语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UNSIGNED)) FROM selected_items WHERE invoiceNo RLIKE 'IN'' at line 1.检查与您的 MySQL 服务器版本对应的手册,了解在第 1 行的“UNSIGNED)) FROM selected_items WHERE invoiceNo RLIKE 'IN''附近使用的正确语法。

And I am using similar approach for estimate:我正在使用类似的方法进行估算:

SELECT Max(CAST (SUBSTR(invoiceNo,3) AS UNSIGNED)) FROM selected_items
WHERE invoiceNo RLIKE 'ES';

How can I do both the operations in one query?如何在一个查询中执行这两项操作? Any help is appreciated.Thank you.任何帮助表示赞赏。谢谢。

As Suggested by @FuzzyTree and @TimBiegeleisen you can try the following query . 根据@FuzzyTree和@TimBiegeleisen的建议,您可以尝试以下查询。

Select (SELECT MAX(CAST(SUBSTR(TRIM(invoiceNo),3) AS UNSIGNED))
FROM selected_items 
WHERE invoiceNo RLIKE 'IN') as maxIN,
(SELECT MAX(CAST(SUBSTR(TRIM(invoiceNo),3) AS UNSIGNED))
FROM selected_items 
WHERE invoiceNo RLIKE 'ES') as maxES;

I believe that your invoiceNo column has some extra characters which are still present even after you use SUBSTR() . 我相信您的invoiceNo列包含一些额外的字符,即使您使用SUBSTR()之后,这些字符仍然存在。 This is causing the CAST to fail because non numeric characters are still present. 这会导致CAST失败,因为仍然存在非数字字符。 If these extra characters be whitespace, then the TRIM() function might come in handy: 如果这些额外的字符为空格,则TRIM()函数可能会派上用场:

SELECT MAX(CAST(SUBSTR(TRIM(invoiceNo),3) AS UNSIGNED))
FROM selected_items 
WHERE invoiceNo RLIKE 'IN'

As @Tah pointed out, here is a link which might help you to remove all alphanumeric characters, if it comes to that: 正如@Tah指出的那样,如果涉及到以下内容,则此链接可以帮助您删除所有字母数字字符:

MySQL strip non-numeric characters to compare MySQL剥离非数字字符进行比较

Update: 更新:

If you want to get both max values in one query, one way would be to do a UNION of the two queries you mentioned: 如果要在一个查询中获得两个最大值,一种方法是对您提到的两个查询进行UNION

SELECT 'invoiceMax', MAX(CAST(SUBSTR(TRIM(invoiceNo),3) AS UNSIGNED))
FROM selected_items 
WHERE invoiceNo RLIKE 'IN'
UNION
SELECT 'estimateMax', MAX(CAST(SUBSTR(TRIM(invoiceNo),3) AS UNSIGNED))
FROM selected_items 
WHERE invoiceNo RLIKE 'ES'

You can combine your queries using conditional aggregation: 您可以使用条件聚合来组合查询:

select
    max(case when invoiceNo rlike 'IN' then (your value here) end) maxIn,
    max(case when invoiceNo like 'ES' then (your value here) end) maxEs
from selected_items
where invoiceNo rlike 'IN'
or invoiceNo like 'ES'

Another way is to use subqueries. 另一种方法是使用子查询。 I'm not sure which runs faster, so you might want to test both. 我不确定哪个运行速度更快,因此您可能要同时测试两者。

select
    (your in query) as maxIn,
    (your en query) as maxEn
// get max index value   codeinigter
        $res = $this->db->get($table)->result();
        $arr = array();
        foreach ($res as $val) {
            $str = preg_replace('/\D/', '', $val->id);
            $arr[] = $str;
        }
        $or = max($arr) + 1;

Try this:尝试这个:

SELECT MAX(invoiceNo) FROM public.selected_items WHERE LENGTH(invoiceNo) = (SELECT MAX(LENGTH(invoiceNo)) FROM public.selected_items)

It should work for PostgreSQL also.它也应该适用于 PostgreSQL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM