一个SQL表tr_dind的内容如下:
hk_bh
----------
005D001A
005D002A
005D003A
005D001S
005D002S
034D001A
034D002A
034D001S
034D002S
034D003S
034D004S
071B002A
071B003A
071B001S
071B002S
071B003S
071B004S
.......
hk_bh字段为8位,前4位代表客户编号,中间3位为每一客户及的流水编号,最后一位是类别.
现在我要找出某一客户(例如034D)S类订单的最后一个流水号(以上举例中为004),请问这样的SQL语句应该如何写?
select top 1 * from tr_dind where hk_bh like 034D% order by substring(hk_bh,4,3) desc
或:
select max(substring(hk_bh,4,3)) from tr_dind where hk_bh like 034D%
select max(substring(hk_bh,4,3)) from tr_dind
大力已经回答的问题,我就凑凑热闹好了,呵呵
select max(substring(hk_bh,4,3)) from 表 where hk_bh like 034D%
select max(substring(hk_bh,5,3)) from tr_dind where hk_bh like 034D%