6

我的数据库表列值是

tenant_ id  group_id
2           2-100
2           2-111
1           1-222
1           1-888
2           2-999 
2           2-1000

询问 :

select max(group_id) from prospect_group where tenant_id=2

我已经使用上面的查询来获取tenant_id=2 的最大值,但它返回的值是 999 而不是 1000。如何获得 1000 作为最大值。???谁能帮我..???

4

2 回答 2

5

你需要有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

通过替换为空字符来尝试它。

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

SQLFiddle 演示

于 2012-10-06T06:01:14.600 回答
1

您需要添加GROUP BY子句。

select max(group_id) from prospect_group where tenant_id=2 group by tenant_ id 
于 2012-10-06T06:01:39.020 回答