[英]mySql Max function ot returning exact result
I don't know why i am not getting the exact result 我不知道为什么我得不到确切的结果
SELECT MAX(MID(order_id,3,20)) As Id FROM `tbl_orders` WHERE `domain_id`=2
+------------+
| id |
+------------+
| 10121452 |
+------------+
Even i tried the same function without MID function 即使我尝试了没有MID功能的相同功能
SELECT MAX(order_id) As Id FROM `tbl_orders` WHERE `domain_id`=2
+------------+
| id |
+------------+
| Hy10121452 |
+------------+
any my database have highest order 任何我的数据库都有最高订单
+--------+------------+
| id | order_id |
+--------+------------+
| 1 | Hy10121452 |
| 2 | Hy10121453 |
| 3 | Hy10121454 |
| 4 | Hy10121455 |
| 5 | Hy10121456 |
| 6 | Hy10121457 |
| 7 | Hy10121458 |
| 8 | Hy10121459 |
| 9 | Hy10121460 |
+--------+------------+
i have to increment in the highest number to generate new order No. Is i am doing something wrong? 我必须增加最高数字以生成新订单号。我做错了吗?
Change MAX(MID(order_id,3,20))
to MAX(MID(order_id,3,))
将
MAX(MID(order_id,3,20))
更改为MAX(MID(order_id,3,))
Syntax:from W3School 语法:来自W3School
SELECT MID(column_name,start[,length]) AS some_name FROM table_name;
where 哪里
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
由于您需要最高ID,因此您可以使用order by DESC
和limit
order by DESC
SELECT order_id As Id FROM `tbl_orders` WHERE `domain_id`=2 ORDER BY order_id DESC LIMIT 1
Problem Solved i just have to rectify Order_id column it contains the duplicate entries. 解决问题我只需要纠正它包含重复条目的Order_id列。 duplicate entries removed and problem solved like a charm.
删除重复的条目,解决问题就像一个魅力。
检查你的数据库 - > table->列数据不包含相同的值如bc1 abc2 abc3 xxx1如果你的系列不同那么结果总是错的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.