Get least 10 values in mysql query - mysql

I have a table in mysql. Table Name is constitutive_table, it contains more than 40 columns and its type is varchar, it contains more than 25000 records. I wrote the query like this to get the 10 least value. But it showing like as you have seen in the picture.
SELECT `Sequence_Name`
, `Name_of_the_Protein`
, `Brain`
FROM `constitutive_table`
where `Brain` != 0
ORDER
BY cast(Brain AS int)
LIMIT 0,10

The data in the Brain column appears to be floating point, so you should be casting to the appropriate type:
SELECT Sequence_Name, Name_of_the_Protein, Brain
FROM constitutive_table
WHERE CAST(Brain AS DECIMAL(14, 8)) <> 0
ORDER BY CAST(Brain AS DECIMAL(14, 8))
LIMIT 10
Most likely what is happening now is that the 10 values you see all have the same value when cast to integer. As a result, MySQL is using some secondary sort to generate the order you do see.
While the above query may resolve your problem, ideally you should change the Brain column to some numeric type.

Related

SQL query does not perform DESC correctly

I have a problem that I do not know how to solve.
I have this query:
$sql = "SELECT * FROM transfer_centre WHERE status = 'out' ORDER BY pris DESC";
And I have four values as pris in my db:
0, 4, 8, 26
So, as the query states they should be listed as such:
26
8
4
0
But somehow it sees the 26 value as 2.6 (i guess?) and therefore 26 is between 4 and 0?
Can someone help me with this issue?
It looks like your column is a VARCHAR type and it is ordering like it. When you order a VARCHAR, you go from left to right on each character, so 26 is between 0 and 4.
You can try this, if you can't change your schema:
SELECT * FROM transfer_centre WHERE status = 'out'
ORDER BY CAST(pris as SIGNED INTEGER) DESC
NOTE: This is not even close from an ideal solution! If the column is treated as a number, you should change the schema applying the right type.

MySQL sorting with alphanumeric prefix

I've got a database with a column that contains the following data:
aaa-1
aaa-2
aaa-3
...
aaa-10
aaa-11
...
aaa-100
aaa-101
...
aaa-1000
When I query and sort the data in ascending order, I get:
aaa-1
aaa-10
aaa-11
...
aaa-100
aaa-101
...
aaa-1000
...
aaa-2
...
aaa-3
Is this actually the correct (machine) way of sorting? Is the order being screwed up because of the aaa- prefix? How do I go about sorting this the way a human would (ie something that looks like the first snippet)?
P.S. If the problem does lie in the prefix, is there a way to remove it and sort with just the numeric component?
P.P.S. It's been suggested to me that I should just change my data and add leading zeroes like aaa-0001 and aaa-0002, etc. However, I'm loathe to go that method as each time the list goes up an order of 10, I'd have to reformat this column.
Thank you all in advance! :)
You can extract the number part, convert it to numeric data type and then do an ORDER BY:
SELECT mytable.*,
CAST(SUBSTRING_INDEX(mycolumn, '-', - 1) AS UNSIGNED) mycolumnintdata
FROM
mytable
ORDER BY mycolumnintdata;
If there are expressions which does not match number, the CAST function would return 0 and those records would be displayed first. You may handle this separately if needed.
I had a similar issue and the trick that did it for me was this one
*"ORDER BY LENGTH(column_name), column_name
As long as the non-numeric part of the value is the same length, this will sort 1 before 10, 10 before 100, etc."*
as given by Andreas Bergström on this question.
Hope that helps someone.
this is the alphabetical order,
you want numerical order,
for do this you must in the ORDER BY clause
trim the costant "aaa-" part
convert it in number
convert(SUBSTRING(val, 3), integer)
I will give you a sample sorting. Not based on your data sample, but this could help you out.
Say you have data like this :
id
----
1
2
6
10
13
when you do ORDER BY id ASC would return :
id
----
1
10
13
2
6
I suggest, use LPAD.
This query : SELECT LPAD('12',5,'0') return 00012
So when you have table data like I provide above, you can sort them like this :
SELECT * FROM TABLE
ORDER BY LPAD(ID,7,'0') ASC
Based on your data.
SELECT SUBSTR('aaa-100',5,LENGTH('aaa-100') - 3) return 100
So, SELECT LPAD( SUBSTR('aaa-100',5,LENGTH('aaa-100') - 3), 7, '0') return 00000100
So you can combine string function such as SUBSTR and LPAD. Do have any clue now?

Preference for rows where field is different from "x"

I want to query my database so that I don't order by or order by desc but instead seek rows when a given field is different from 0 first and only after get others ordered by if there are no rows with field != "0". What is the best way to accomplish this or even is it possible to do so?
This is supposing the field can have values from -100 to 0 to 100
With example:
Considering LIMIT equals 3 and field in consideration is field1
field1
10
0
-20
-40
Another set:
field1
0
0
-100
50
In the first set, the results extracted will be 10, -20 and -40 (by any order), while in the second set they should be -100, 50 and 0 (any of the zeros, by any order).
First I'd like to check whether there exists != 0 in the database and if true extract those and only after 0's until I fill the LIMIT
something like this?
its not clear AT ALL what you want so I'm only guessing here.
I want to query my database so that I don't order by or order by desc but instead seek rows when a given field is different from 0
seek rows to me means find all rows that are not 0.
only after get others ordered by if there are no rows with field != "0".
to me that means order the rows in a sub query where the field is not 0.
with that in mind maybe this query?
SELECT * FROM table t
WHERE field IN (
SELECT field
from table t
WHERE field <> 0
ORDER BY field
)
without data It's not clear what you want. but i think something like this is what you were asking.. you could also try
SELECT * FROM table t
WHERE field <> 0
ORDER BY field

Select data which have same letters

I'm having trouble with this SQL:
$sql = mysql_query("SELECT $menucompare ,
(COUNT($menucompare ) * 100 / (SELECT COUNT( $menucompare )
FROM data WHERE $ww = $button )) AS percentday FROM data WHERE $ww >0 ");
$menucompare is table fields names what ever field is selected and contains data bellow
$button is the week number selected (lets say week '6')
$ww table field name with row who have the number of week '6'
For example, I have data in $menucompare like that:
123456bool
521478bool
122555heel
147788itoo
and I want to select those, who have same word in the last of the data and make percentage.
The output should be like that:
bool -- 50% (2 entries)
heel -- 25% (1 entry)
itoo -- 25% (1 entry)
Any clearness to my SQL will be very appreciated.
I didn't find anything like that around.
Well, keeping data in such format probably not the best way, if possible, split the field into 2 separate ones.
First, you need to extract the string part from the end of the field.
if the length of the string / numeric parts is fixed, then it's quite easy;
if not, you should use regular expressions which, unfortunately, are not there by default with MySQL. There's a solution, check this question: How to do a regular expression replace in MySQL?
I'll assume, that numeric part is fixed:
SELECT s.str, CAST(count(s.str) AS decimal) / t.cnt * 100 AS pct
FROM (SELECT substr(entry, 7) AS str FROM data) AS s
JOIN (SELECT count(*) AS cnt FROM data) AS t ON 1=1
GROUP BY s.str, t.cnt;
If you'll have regexp_replace function, then substr(entry, 7) should be replaced to regexp_replace(entry, '^[0-9]*', '') to achieve the required result.
Variant with substr can be tested here.
When sorting out problems like this, I would do it in two steps:
Sort out the SQL independently of the presentation language (PHP?).
Sort out the parameterization of the query and the presentation of the results after you know you've got the correct query.
Since this question is tagged 'SQL', I'm only going to address the first question.
The first step is to unclutter the query:
SELECT menucompare,
(COUNT(menucompare) * 100 / (SELECT COUNT(menucompare) FROM data WHERE ww = 6))
AS percentday
FROM data
WHERE ww > 0;
This removes the $ signs from most of the variable bits, and substitutes 6 for the button value. That makes it a bit easier to understand.
Your desired output seems to need the last four characters of the string held in menucompare for grouping and counting purposes.
The data to be aggregated would be selected by:
SELECT SUBSTR(MenuCompare, -4) AS Last4
FROM Data
WHERE ww = 6
The divisor in the percentage is the count of such rows, but the sub-stringing isn't necessary to count them, so we can write:
SELECT COUNT(*) FROM Data WHERE ww = 6
This is exactly what you have anyway.
The divdend in the percentage will be the group count of each substring.
SELECT Last4, COUNT(Last4) * 100.0 / (SELECT COUNT(*) FROM Data WHERE ww = 6)
FROM (SELECT SUBSTR(MenuCompare, -4) AS Last4
FROM Data
WHERE ww = 6
) AS Week6
GROUP BY Last4
ORDER BY Last4;
When you've demonstrated that this works, you can re-parameterize the query and deal with the presentation of the results.

mysql sort varchar with numbers

How can I sort this data numerically rather than lexicographically?
100_10A
100_10B
100_10C
100_11A
100_11B
100_11C
100_12A
100_12B
100_12C
100_13A
100_13B
100_13C
100_14A
100_14B
100_14C
100_15A
100_15B
100_15C
100_16A
100_16B
100_16C
100_1A
100_1B
100_1C
100_2A
100_2B
100_2C
100_3A
100_3B
100_3C
100_4A
100_4B
100_4C
100_5A
100_5B
100_5C
100_6A
100_6B
100_6C
100_7A
100_7B
100_7C
100_8A
100_8B
100_8C
100_9A
100_9B
100_9C
select generalcolum from mytable order by blockid, plotid ASC
What I need out of this sort order is
100_1A
100_1B
100_1C...
...
...
100_10A
100_10B
100_10C
What I need to do in some way is have a zero added before the sort happens so that, I can get them in the order I want.
There are two colums, one that stores the 100 (number before the underscore) and one that stores the 1A the value after the underscore.
My sudo crap select
select thiscolum this table
order by blockid, plotid(+1 zero to prefix if len(plotid) < 2)
For example if the plot value is 1A, to do the best sorting, i need it to be looked at as 01A so that it comes before 10A.
order by length(blockid), blockid, length(plotid), plotid