mySql Max function ot returning exact result - mysql

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
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?

check your database -> table-> column the data does not contain the same values like this abc1 abc2 abc3 xxx1 if you differnt series then the result always wrong

Change MAX(MID(order_id,3,20)) to MAX(MID(order_id,3,))
Syntax:from 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

Since you want the highest id then you can use order by DESC and limit
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. duplicate entries removed and problem solved like a charm.

Related

Find the max value in the last digit

I am using MySQL 5.5. I am facing a problem on how to find the max value in the last digit.
For example below the table, I want to get the max value is detected the last digit. The result should be 100-1-15
Table name: abc
+----+------------+
| id | code |
+----+------------+
| 1 | 100-1-1 |
| 2 | 100-1-2 |
| 3 | 100-1-15 |
| 4 | 100-1-6 |
| 5 | 100-1-3 |
| 6 | 100-1-5 |
| 7 | 100-1-9 |
+----+------------+
I am using below the SQL query, but doesn't work:
SELECT id,max(code) FROM abc;
Hope someone can guide me how to solve it and can get the max code is 100-1-15. Thanks.
SELECT *
from abc
order by SUBSTRING_INDEX(code, '-', -1) + 0 desc
limit 1
Try
Select
id,
code
from abc
order by max(CAST(SUBSTR(code, 7, LENGTH(code)-6) AS SIGNED))
limit 1;

Sort Table that primary key start with letter and get last record

this my table
+---------+-----------+
| Item_No | Item_name |
+---------+-----------+
| IL1 | abc_1 |
| IL2 | abc_2 |
| IL3 | abc_3 |
| IL4 | abc_4 |
| IL5 | abc_5 |
| IL6 | abc_6 |
| IL7 | abc_7 |
| IL8 | abc_8 |
| IL9 | abc_9 |
| IL10 | abc_10 |
| IL11 | abc_11 |
+---------+-----------+
I want to Sort my Table by primary key I try this SQL
SELECT *
FROM `item_master`
WHERE Item_No LIKE 'IL%'
ORDER by I_code DESC LIMIT 1
but I gives me IL9 as last one
Your could, for example, extract number from "Item_No"(I_code?) and convert it to int:
SELECT *
FROM item_master
WHERE Item_No LIKE 'IL%'
ORDER by cast(substring(Item_No, 3) as UNSIGNED) DESC LIMIT 1
The issue that you are facing here is a wrong ordering by VARCHAR I assume, which is a string and it gets ordered lexically, here is a way to fix it:
SELECT *
FROM item_master
ORDER by right(concat('0000', Item_No), 4) DESC LIMIT 1
Notice the 0000 (4 zeros) and right() function second argument 4 is the character length for the ordering field
Another way to solve it to use LPAD
SELECT *
FROM item_master
ORDER by lpad(Item_No, 100, '0') DESC LIMIT 1
here is a working sqlfiddle to check

SQL - Select column with certain value in it and other random values

Let me say i have a table called test with the following data
+---------+-----------------+
| id | t_number |
+---------+-----------------+
| 1 | 864291100247345 |
| 2 | 355488020906457 |
| 3 | 864296100098739 |
| 4 | 864296100098325 |
| 5 | 864296100119956 |
What i want to do is to be able to write a select statement that returns a 3 rows with two random values and one mandatory value from the t_number column
for example if the mandatory value is 864291100247345 the output should something like below
+---------+-----------------+
| id | t_number |
+---------+-----------------+
| 1 | 864291100247345 |
| 2 | 355488020906457 |
| 4 | 864296100098325 |
OR
+---------+-----------------+
| id | t_number |
+---------+-----------------+
| 1 | 864291100247345 |
| 3 | 864296100098739 |
| 4 | 864296100098325 |
I have tried the below query but it's not yielding the output i expect, in a sense that it does return a result but without the mandatory value
SELECT * FROM test WHERE t_number = 864291100247345 OR id LIMIT 3;
What is the best way to go about this?
Thank you.
You can use order by:
SELECT t.*
FROM test
ORDER BY (t_number = 864291100247345) DESC,
rand()
LIMIT 3;
This returns the mandatory number first and then random numbers after that.
MySQL treats boolean values (the result of the = expression) as numbers in a numeric context, with "1" for true and "0" for false. So the first expression in the order by sorts the result set with the "true" conditions first, followed by the others.

MySQL - add text prefix before column name in SELECT statement

Here is my table:
| ID | NUMBER |
| 1 | 523 |
| 2 | 293 |
| 3 | 948 |
And now, I want to get all NUMBER values but I want to add in result two numbers - 48 - (without upadting existing results). So finally I want print these results:
| NUMBER |
| 48523 |
| 48293 |
| 48948 |
So I need a query, something like this:
SELECT '48' + `number` FROM `table`
but this query doesn't work fine (this query only update column name from NUMBER to 48 + NUMBER).
Any ideas?
Thanks.
You need CONCAT
SELECT CONCAT('48' , `number`) AS number FROM table
Demo

How to update rows of MySQL without stored function?

My cloud server doesn't allow stored function/procedure, so how to do this?
My table was:
|id |Status |
| 1 | Good |
| 2 | Badd |
| 3 | Good |
| 4 | Good |
I wanted it to be:
| 1 | Good |
| 3 | Badd |
| 4 | Good |
| 5 | Good |
Can this be done with one line of statement? Basically I want to change all id in rows where id>x to id+1. In this case, x = 2.
Update
Forgot to mention that id is unique so how to sort them desc then change?
You can use
UPDATE yourtable SET id = id + 1 WHERE id > 1 ORDER BY ID DESC;
there is a known "bug" or "feature" that you will face if id is unique but order by desc will help you to avoid it.
Try this
UPDATE `table` SET id = id + 1 WHERE id > 1