Cluster difficulty and Where clause in Mysql

Asked

Viewed 79 times

3

I need to search the database for the last 4 most recent records, the column being exr_exa_id is in agreement with the informed Ids, and exr_exa_id may not be duplicated.

I tried to group in some ways, but I was unsuccessful, because either it does not display the most recent records, or it displays equal records in the column exr_exa_id (see image).

Can you help me?

SELECT * 
FROM exa_exameresultado 
WHERE 
   exr_exa_id IN (18,19,20,71) AND 
   exr_pac_id = 2128 
ORDER BY 
   exr_data DESC, 
   exr_exa_id ASC 
LIMIT 4

inserir a descrição da imagem aqui

  • 1

    Put it on http://sqlfiddle.com/, so you can help us help you :)

  • 1

    follow link http://sqlfiddle.com/#! 9/58d88/1/0

  • When placing the group by, it changes the date... Ex: the + recent date for the exr_exa_id field of value 18 is 2015-04-09. When placing a group by in exr_exa_id, it changes the date of this record to 2014-10-07

1 answer

1

SELECT 
  * 
FROM 
  exa_exameresultado 
WHERE 
  exr_exa_id IN (18,19,20,71) 
AND 
  exr_pac_id = 2128 
GROUP BY 
  exr_exa_id
ORDER BY 
   exr_data DESC, exr_exa_id ASC 
LIMIT 4

The only change I made was putting one GROUP BY exr_exa_id. But the GROUP BY works in this case because the other fields are equal in their values as well. If after their values change, maybe this SELECT no longer serve you.

Browser other questions tagged

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