mySql Max函数没有返回确切的结果

时间:2015-02-21 06:20:59

标签: mysql

我不知道为什么我没有得到确切的结果

SELECT MAX(MID(order_id,3,20)) As Id FROM `tbl_orders` WHERE `domain_id`=2 
+------------+
|   id       |
+------------+
|  10121452  |
+------------+

即使我尝试了没有MID功能的相同功能

SELECT MAX(order_id) As Id FROM `tbl_orders` WHERE `domain_id`=2 
+------------+
|   id       |
+------------+
| Hy10121452 |
+------------+

我的任何数据库都有最高订单

+--------+------------+
| id     |  order_id  |
+--------+------------+
| 1      | Hy10121452 |
| 2      | Hy10121453 |
| 3      | Hy10121454 |
| 4      | Hy10121455 |
| 5      | Hy10121456 |
| 6      | Hy10121457 |
| 7      | Hy10121458 |
| 8      | Hy10121459 |
| 9      | Hy10121460 |
+--------+------------+

我必须以最高数字递增以生成新订单号 我做错了吗?

4 个答案:

答案 0 :(得分:0)

MAX(MID(order_id,3,20))更改为MAX(MID(order_id,3,))

语法:来自W3School

 SELECT MID(column_name,start[,length]) AS some_name FROM table_name; 

,其中

column_name   Required. The field to extract characters from
start         Required. Specifies the starting position (starts at 1)
length        Optional. The number of characters to return. If omitted, the MID() function returns the rest of the text

答案 1 :(得分:0)

由于您需要最高ID,因此您可以使用order by DESClimit

SELECT order_id As Id FROM `tbl_orders` WHERE `domain_id`=2 ORDER BY order_id DESC LIMIT 1

答案 2 :(得分:0)

问题解决了我只需要纠正它包含重复条目的Order_id列。删除重复的条目,解决问题就像魅力一样。

答案 3 :(得分:0)

检查您的数据库 - >表 - >列数据不包含相同的值如此 bc1 abc2 abc3 xxx1 如果您的系列不同则结果总是错误的