Skip to content
Advertisement

Get least 10 values in mysql query

Database table.

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

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement