我的数据库表列值是
tenant_ id group_id
2 2-100
2 2-111
1 1-222
1 1-888
2 2-999
2 2-1000
Run Code Online (Sandbox Code Playgroud)
查询:
select max(group_id) from prospect_group where tenant_id=2
Run Code Online (Sandbox Code Playgroud)
我已经使用上面的查询来获取tenant_id = 2的最大值,但它返回的值为999而不是1000.如何获得1000作为最大值.??? 谁能帮我..???
你需要有GROUP BY条款
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
Run Code Online (Sandbox Code Playgroud)
通过替换为空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
Run Code Online (Sandbox Code Playgroud)