How to use SQL min() with float?

Asked

Viewed 412 times

1

I have the following database table:

 *Table structure for table `resultados` */
    DROP TABLE IF EXISTS `resultados`;

    CREATE TABLE `resultados` (

      `cpf` varchar(14) NOT NULL,
      `nome` varchar(255) DEFAULT NULL,
      `codcurso` int(5) DEFAULT NULL,
      `nota` float DEFAULT NULL,
      PRIMARY KEY (`cpf`)

);

/*Data for the table `resultados` */

insert  into `resultados`(`cpf`,`nome`,`codcurso`,`nota`) values 
('123.456.789-01','Alexandre Mie Lopes Mesa',78,4.21),
('123.876.332-72','Maria Ximenes Rosa',1,9.16),
('222.333.444-55','Livia Fernandes Linderberg',43,9.87),
('232.234.789-77','Karol Linderberg',43,8.92),
('289-890-912-76','Pedro Solano Susp',16,4.21),
('345.678.900-37','Amilton Pedro da Silva',78,9.98),
('432.654.987-21','Julio Martinez Silva',21,7.34),
('454.098.123-45','Luiz Henrique de Souza',1,7.35),
('765.123.098-22','Juliana Lopes Alves',22,5.67),
('903.201.871-23','Luiz Peres Lopes',16,8.77),
('987.654.321-10','Ana Alves de Souza',78,9.12);

And I want to select the minimum value, when I use only:

SELECT min(nota) AS menor FROM resultados

I get the result (4.21), but if I want to create a table, with students with lower grades I can’t get such a table

SELECT * FROM resultados WHERE nota = 4.21

I also tried to save this value as float and pass as var but it was not possible, I could only do with the following code:

SELECT * FROM resultados WHERE nota like '" + menorR + "'"

where less R = 4.21

Soon I want to understand why the Where note = (float number) did not work

  • Instead of using float because you don’t use decimal? \note` DECIMAL(10,2) DEFAULT NULL`, for example

  • SELECT * FROM results WHERE note = (SELECT min(note) AS minor FROM results) what returns ?

  • @Jorgeb. Out of curiosity: What is the difference of Float and Decimal?

  • 2

    @Andrey - Decimal stores the exact value.. ex: 4.21... With float/double (floating point), it could store something like 4.2099999... i.e.. in the future, you may have some problems with rounding.

  • 1

    @Andrey The difference is accuracy of the numbers that are saved. With Float does not guarantee the accuracy of the numbers. With Decimal ensures accuracy but loses decimal places. Soen fountain

1 answer

1

This here works:

SELECT *
FROM resultados
WHERE nota = ( SELECT min(nota) AS menor
               FROM resultados )

Check on the Sqlfiddle: http://sqlfiddle.com/#! 2/6ebfe/4 (sometimes gets off the air for a while =( )

The problem is precision: He won’t find 4.210000038147 if you do only WHERE nota = 4.21.


As @Jorgeb. said well in the comments of the question, the ideal would be to use DECIMAL instead of FLOAT, because the DECIMAL you set the accuracy. For example: three house before the comma and two houses after the comma:

CREATE TABLE `resultados` (
  `cpf` varchar(14) NOT NULL,
  `nome` varchar(255) DEFAULT NULL,
  `codcurso` int(5) DEFAULT NULL,
  `nota` decimal(3, 2) DEFAULT NULL,
  PRIMARY KEY (`cpf`)
);

In decimal(3, 2), the first number shall be the boxes before the comma and the second the boxes after the comma.

Done this the version below should work (credits: @Jorgeb.):

SELECT * FROM resultados WHERE nota = 4.21
  • 1

    +1 very good answer. It remains to add that if it is with decimal can already use the version SELECT * FROM resultados WHERE nota = 4.21.

  • 1

    @Jorgeb. Updated =)

  • Actually in the float it is also possible to select the presition of the same, what changes between one and the other is that the float uses a binary representation of the number, while the Decimal uses the numbers in a decimal notation. That’s why the float cannot represent exactly the numbers assigned to it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.