3

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? I would also like it to be cast into an integer like 1000.

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; 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.

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.

Ceeee
  • 1,392
  • 2
  • 15
  • 32
AndroidNewBee
  • 744
  • 3
  • 12
  • 36

5 Answers5

2

As Suggested by @FuzzyTree and @TimBiegeleisen you can try the following query .

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;
1

I believe that your invoiceNo column has some extra characters which are still present even after you use SUBSTR(). This is causing the CAST to fail because non numeric characters are still present. If these extra characters be whitespace, then the TRIM() function might come in handy:

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:

MySQL strip non-numeric characters to compare

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:

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'
Community
  • 1
  • 1
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
1

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
FuzzyTree
  • 32,014
  • 3
  • 54
  • 85
0
// 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;
  • 1
    Can you add some explanation to your code such that others can learn from it? Additionally, can you explain why you introduce PHP code to a question that is already answered multiple times with pure MySQL queries? – Nico Haase Sep 07 '18 at 07:52
0

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.

cursorrux
  • 1,382
  • 4
  • 9
  • 20
Nabyendu
  • 1
  • 2