My DB table column values are
tenant_ id group_id
2 2-100
2 2-111
1 1-222
1 1-888
2 2-999
2 2-1000
Query :
select max(group_id) from prospect_group where tenant_id=2
I have used the above query to get the max value for tenant_id=2 but it returns the value as 999 instead of 1000. How to get 1000 as the max value.??? Can anyone help me..???
The MAX() function returns the largest value of the selected column.
MySQL MAX() Function The MAX() function returns the maximum value in a set of values.
The MAX() function can be used on the string column. For example, the following uses the MAX() function on the LastName column of the Employee table. It will sort the column alphabetically and the last value will be returned.
You need to have GROUP BY
clause
SELECT tenant_ID, MAX(CAST(group_ID AS SIGNED))
FROM tableName
-- WHERE tenant_id=2 -- uncomment this if you select only for specific tenant_ID
GROUP BY tenant_ID
try it by replacing to an empty char.
SELECT tenant_ID,
MAX(CAST(REPLACE(group_ID, CONCAT(tenant_ID, '-'), '') AS SIGNED)) maxGID
FROM tableName
-- WHERE tenant_id=2 -- uncomment this if you select only for specific tenant_ID
GROUP BY tenant_ID
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With