sorting alphanumeric in mysql? [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
040, 044P, 041BL, 041W, 041PB
^^ This is the order it is coming out in by using Order By clause.
I think this is how it should appear instead:
040, 041BL, 041PB, 041W, 044P
I know similar questions must have been asked before, but I still can't figure out anything!
Edit: After X.L.Ant's comment, I realized my mistake. Therefore, simple order by clause is working for the test case given above. However, the case is still complicated if the number of digits is not always going to be 3 as GolezTrol mentioned. What should one do in that case?

Try:
SELECT string,
#num := CONVERT(string, signed) AS num_part,
Substring(Trim(LEADING '0' FROM string), Length(#num) + 1) AS rest_of_string
FROM table1
ORDER BY num_part,
rest_of_string
This way, the numbers will still be ordered by their numerical value (the leading 0s not being taken into account).
See fiddle.

try LPAD() function - but remeber that LPAD trims digits.
http://sqlfiddle.com/#!2/d7281/3/0

Related

PHP mysql query taking too long for using like to timestamps [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm using this query :
select field from table1 where time LIKE '%2016-03%' order by time asc limit 1
My problem is that the table has thousands of rows and so it takes too long to search for 1 row. Is it normal?
LIKE queries are always going to be slower than looking for a specific value.
It'll help immensely to add an INDEX on the field and change your query to LIKE '2016-03%' (there won't ever be anything before the year in a timestamp so drop that first %). It'll be able to take a couple shortcuts, at least.
If you use LIKE and starts with % MySQL must make a FULL TABLE SCAN to find the correct Dates, but if you start direct with the year they can use a index : ... KIKE '2016-03-%'
Try adding an INDEX to your time column and as other have pointed out, remove the leading % from the query.
See this post for more info

Get the closest value of a column in MYSQL [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a table that have a CP value (numeric), for example, 28030, 28060, 27100 etc. And the user can introduce a number via PHP. I want to, having this number for example, 28050, order in MYSQL my table putting 28060 as the first position.
This is the basic of my table:
SELECT * FROM `tiendas` ORDER BY `CP`
ABS() will work. Here's a query that does the job:
SELECT
CP
FROM tiendas
ORDER BY ABS(CP- 28050) ASC

how to get best time from table? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How to get best time from MySql table collumn which I made varchar (I can change it, just I didn't know better) which data looks like "00:05:22.22" (it's "hours:minutes:seconds.milliseconds").
it's like laps times in that collumn..
or I should change my DB table structure? to what?
and how I should do it then?
i.e.
in that collumn are records like this:
00:00:04.99
00:00:04.57
00:00:04.55
00:00:04.58
00:00:03.36
And I would like to get all of them ordered by shortest time. Like best lap.
If you are using MySQL 5.6.4 or greater, you can use the TIME datatype for your column and indicate that it should accept fractional parts of a second - see here.
Otherwise, using a VARCHAR column, you can still use the MIN/MAX functions for your data and expect to get a valid result (provided all values are correctly formatted), since alphabetically sorting the data in your use case should give the same result as numerically sorting it.
Alternatively, you can use the ORDER BY clause on that column and take just the first n results. Depends on your needs.
To get an ordered set from a table your query needs to specify an order -
SELECT *
FROM `table`
ORDER BY `lap_time` ASC
You do not have to include ASC in the query as any ORDER BY will sort from smallest to largest unless otherwise told not to do so with DESC
As you have said, your column datatype is varchar, you can do
select [column_names] from [table_name] order by [column_name_containing_time] asc
You can see an example here: SQL Fiddle

MySQL SELECT multipule rows; based on variables [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a database that looks similar to this:
ID | MESSAGE | TO | FROM | DATE
I need to query this database to check if NameA matches TO or NameB matches FROM. I also want to display these in order from newest to oldest (based on date).
mysqli_query($link, `SELECT * FROM [table] WHERE `to`='[NAMEA]' OR `from`='[NAMEB]' ORDER BY `date` DESC
That's the short answer. Sure, it can be improved, but this will depend on the structure of your table, the type of function you are using to access your database, etc.
Keep in mind: Do NOT use mysql_* functions. They are deprecated and may contain security vulnerabilities. Use mysqli_* or PDO. More documentation on these functions can be found at php.net.

php numbers sort from mysql asc [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I need to get order numbers by ASC.
In my mysql table It written with - (ex. 1-2; 2-3; 15-60 etc.)
Now I get order:
15-60; 1-2; 2-3;
why not 1-2; 2-3; 15-60?
I'm making new sorting in my opencart.
Tutorial to make new sorting I used: sort by size opencart
You can try with:
SELECT *
FROM yourtable
ORDER BY numbers + 0
to automatically cast you VARCHAR column to a number, and apply the order correctly.
Please see fiddle here.
Assuming that your order numbers are in fact strings of digits interspersed with hyphens, you are getting the string sort order by default.
You can cast strings to integers, but perhaps a better approach is to store order numbers appropriately padded by zero digit characters, so that 15-60, 1-2, 2-3 are stored as 015-060, 001-002, 002-003 (or whatever degree of padding is appropriate). Then the string sort order and numeric sort order will be the same, and you don't need to do casting.
you can also use this
SELECT *
FROM yourtable
ORDER BY CAST(numbers as unsigned)
DEMO HERE

Categories