Compare MySQL search results

0

I need to compare the result of a query to not repeat the result.

For example: I have in the unit register your cod and speed, but I can have 2 situations:

1) more than one occurrence for the same unit and different speeds, and need to be listed only the occurrence of the highest speed.

cod  - velocidade
100  - 128
100  - 512

As a result of the precise search, list only the highest value: 100-512

2) I can also have the return of more occurrences with the same information, but I need to list only one of them:

cod - velocidade
105 - 512
105 - 512
105 - 512

I need only list 1x: 105-512

How can I be doing these checks?

    
asked by anonymous 21.11.2014 / 14:51

2 answers

4

Felipe uses SQL aggregation function.

SELECT COD, MAX(VELOCIDADE 
  FROM TABELA
  GROUP BY COD

Ready. Sql will group by code all speeds and display only the largest.

Group by functions

group by

Examples of group by

    
21.11.2014 / 16:39
2

In question 1 you can do the following:

SELECT MAX(velocidade) AS velocidade FROM tabela WHERE cod = 100;

In question 2 you can do the following:

SELECT DISTINCT cod, velocidade FROM tabela WHERE cod = 105 AND velocidade = 512
    
21.11.2014 / 16:31