Mysql sorting using numbers and characters - mysql

I am having a mysql table field with mixed values.Say,
1
2
Priya
Radha
If I sorts this in ascending order, the result will be displayed as shown above and if I sorts in descending order, the result will be
Radha
Priya
2
1
Here Mysql will give priority to numbers and then characters.
What to do, if I want Mysql to give priority to characters?
ie, Sorting in ascending order should give a result like the one below:
Priya
Radha
1
2
Please help

I found this, maybe you can try it.
Sorting characters and numbers in mysql

Related

Select and display by a value of a number

I want to select from my database and to display each value by a value of a number, example:
I have in database this:
Peter 3.80
Maria 5.67
John 2.52
Robert 1.53
I want to display this values but I want to display by example Maria show more times than Robert because she have value of number 5.67 and Robert have 1.53
Output must be Names like Maria, John...
At every refresh show me one name but I need frequency with value number high
I think you are looking for order by. Just use order by col_name, here you may wanna add desc for descending order
Are you searching for sorting the name by the value of number ?
You can try use
SELECT name
FROM your table name
ORDER BY your column name DESC;
You can see reference here to for this Mysql Sort

distinct with highest value using django orm

I have records in a table like that
ID Name priority
1 MyString 12
2 Search 20
3 MyString 50
4 MyString 10
5 Search 7
I want to get distinct rows with highest priority value. For example, the above example should give the following result
ID Name priority
2 Search 20
3 MyString 50
I was going through the docs and found out that distinct on columns cannot be found out on mysql. So I tried to perform a group-by and sort (descending on priority column).
I tried this
model_name.objects.all().values('name','priority','id').annotate(Count('search_name')).order_by('-priority')
But I am not getting the desired result. Is it possible to do this in a single orm query.
I am using django 1.6 and mysql as my database.
Have you tried adding the .distinct() method to your query? Django ORM has the distinct() method. I hope this helps you:
https://docs.djangoproject.com/en/dev/ref/models/querysets/#distinct

Counting the number of pattern occurrences in a certain column with MySQL

I want to search the column Name of a certain table to see how many rows have a Name value that matches a certain pattern. For example, if the pattern I am looking for is %Peter% and in this table there are 5 rows with the Name values:
Peter
Peter Smith
George Peter
Peter Peter
Carl
I want to obtain the value 4. I tried to use COUNT, but don't know how to combine it for example with LIKE. How do I go about to do this?
Not sure why didn't LIKE work for you, but this SHOULD do the trick:
SELECT COUNT(*) FROM table
WHERE name LIKE '%Peter%'

Is there a possibility to change the order of a string with numeric value

I have some strings in my database. Some of them have numeric values (but in string format of course). I am displaying those values ordered ascending.
So we know, for string values, 10 is greater than 2 for example, which is normal. I am asking if there is any solution to display 10 after 2, without changing the code or the database structure, only the data.
If for example I have to display values from 1 to 10, I will have:
1
10
2
3
4
5
6
7
8
9
What I would like to have is
1
2
3
4
5
6
7
8
9
10
Is there a possibility to ad an "invisible character or string which will be interpreted as greater than 9". If i put a10 instead of 10, the a10 will be at the end but is there any invisible or less visible character for that.
So, I repeat, I am not looking for a programming or database structure solution, but for a simple workaround.
You could try to cast the value as an number to then order by it:
select col
from yourtable
order by cast(col AS UNSIGNED)
See SQL Fiddle with demo
You could try appending the correct number of zeroes to the front of the data:
01
02
03
..
10
11
..
99
Since you have a mixture of numbers and letters in this column - even if not in a single row - what you're really trying to do is a Natural Sort. This is not something MySQL can do natively. There are some work arounds, however. The best I've come across are:
Sort by length then value.
SELECT
mixedColumn
FROM
tableName
ORDER BY
LENGTH(mixedColumn), mixedColumn;
For more examples see: http://www.copterlabs.com/blog/natural-sorting-in-mysql/
Use a secondary column to use as a sort key that would contain some sort of normalized data (i.e. only numbers or only letters).
CREATE TABLE tableName (mixedColumn varchar, sortColumn int);
INSERT INTO tableName VALUES ('1',1), ('2',2), ('10',3),
('a',4),('a1',5),('a2',6),('b1',7);
SELECT
mixedColumn
FROM
tableName
ORDER BY
sortColumn;
This could get difficult to maintain unless you can figure out a good way to handle the ordering.
Of course if you were able to go outside of the database you'd be able to use natural sort functions from various programming languages.

sorting numbers stored in text field in MYSQL database

i have a mySQL database with a text field in which is stored a number.
i need to produce a recordset sorted in descending numerical order.
this works fine until we get to numbers greater than 10 ie
9
8
7
6
5
4
3
2
10
1
is there a simple way of sorting this 'correctly' ?
(yes, i know i should have numbers in a numerical field, but i'm working with what i have :))
i'm using the results on an asp/vbscript/jquery page so maybe even a client-side solution is viable...
any suggestions?
ORDER BY ABS(text_column) DESC
Or, if you also have to deal with negative values:
ORDER BY CAST(text_column AS SIGNED) DESC
You need to type cast it to INTEGER using CAST function in MySQL:
ORDER BY CAST(text_column AS UNSIGNED INTEGER)
Try this one -
... ORDER BY text_column * 1