문제

I have a column invoice_id in table invoices. The invoice_id format is

INV-2-43-001-01
INV-2-43-002-01
INV-2-43-002-02

And i want to get the highest value in bold columns.

도움이 되었습니까?

해결책

If this is the format you can get it by two ways,I have used SUBSTRING_INDEX() to get the number and one approach is use the limit with order by

select 
SUBSTRING_INDEX(SUBSTRING_INDEX(Inv,'-',-2),'-',1) inv
FROM Table1
ORDER BY inv DESC
LIMIT 1;

And other is to use the MAX() function

select 
MAX(SUBSTRING_INDEX(SUBSTRING_INDEX(Inv,'-',-2),'-',1)) inv
FROM Table1

Fiddle Demo

다른 팁

You can do it also in this way:

SELECT
REVERSE(SUBSTRING(REVERSE(Inv),4, 3))
FROM Table1

SQLFiddle Demo

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top