Find the max value in the last digit - mysql

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;

Related

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.

How to find max of mysql varchar column

i have a table like this with a var char field reference_number
actually i need to get the max of number in that field
<<student>>
|`id` | `reference_number`(varchar(25))
--------------------------
| 1 | L250
| 2 | SP521
| 3 | S120
| 4 | SP500
| 5 | S122
the desired result is 521 because if we are avoiding the non numeric value then it will come like this
|`id` | `reference_number`
--------------------------
| 1 | 250
| 2 | 521
| 3 | 120
| 4 | 500
| 5 | 122
how to get the the value 521 from the table
I assume you have extracted 'reference_number' as shown in the second snippet from the first snippet. if so, try :::
select max(cast (reference_number as int)) from student
In order to get the number 521 (and all the numbers from the reference_number column) you could try:
SELECT *
FROM yourtable
WHERE reference_number REGEXP '^[0-9]+$';
And then you can add an order by statement.

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

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

Creating Temporary Column Order By Slow

So basically what I'm trying to do is get gained experience, ordering it, then only displaying top 5 or 50. Now note that I'm not SQL expert but I have knowledge of indexes as well as file sorting. The query that I have is filesorting most likely due to "gained_xp" not being a index-- let alone even a column as it's only temporary. There's no clear explanation how to fix this as I'm trying to contain it all in one query. I'm trying to sort nearly 13k rows with that number only expanding. I'd also need the number of rows to be dynamic as well as the time since. Any help would be appreciated. Thank you
Explain Output: Using where; Using temporary; Using filesort
Indexes include: time userid override overallXp overallLevel overallRank
The closest I've gotten to order all rows (which never ends up completing and ends in a mysql reboot) are:
SELECT FROM_UNIXTIME(time, '%Y-%m-%d'), t_u.userid as uid, MAX(t_u.OverallXP)-(SELECT overallXP FROM track_updates WHERE `userid` = t_u.userid AND `time`>'1394323200' ORDER BY id ASC LIMIT 1) as gained_xp
FROM track_updates t_u
WHERE t_u.time>'1394323200'
GROUP BY t_u.userid
If I'd run a query that selects only one user and works correctly is:
SELECT FROM_UNIXTIME(time, '%Y-%m-%d'), (t_u.overallXP)-(SELECT overallXP FROM track_updates WHERE `userid`='1' ORDER BY `id` ASC LIMIT 1) as gained_xp, t_u.userid
FROM track_updates t_u
WHERE t_u.userid='1' AND t_u.time>'1393632000'
ORDER BY t_u.time DESC
LIMIT 1
Sample Data per request:
____________________________________________________________________
| id | userid | time | overallLevel | overallXP | overallRank |
| 1 | 1 | 1394388114 | 1 | 1 | 1 |
| 2 | 1 | 1394389114 | 2 | 10 | 1 |
| 3 | 2 | 1394388114 | 1 | 1 | 2 |
| 4 | 2 | 1394389114 | 1 | 5 | 2 |
| 5 | 2 | 1394390114 | 2 | 7 | 2 |
Output (most recent time; gained xp current-initial; ordered by gain_xp):
____________________________________________
| id | time | userid | gained_xp |
| 1 | March 9th 2014 | 1 | 9 |
| 2 | March 9th 2014 | 2 | 6 |