Longer date search for a specific ID

Asked

Viewed 281 times

1

I have a problem in a SQL where I need to find the biggest date of a ID specific, where it has several records of this same ID:

Dice:

ID - date - name

2 - 10/10/2004 - cesar
5 - 10/10/2008 - Japan
4 - 10/10/2002 - maria
1 - 04/01/2017 - paulo
1 - 12/12/2017 - paulo
4 - 10/10/2000 - Maria
5 - 10/10/2017 - Japan

In this example I would like to seek the ID = 1 which corresponds to the name Paul and I need the highest date in specific, then followed my SQL where I seek the greatest date, but the name comes another and not the ID referent.

SELECT MAX(data) as data, nome FROM teste WHERE id = '1'

Result ... 1 - 12/12/2017 - cesar

Expected ... 1 - 12/12/2017 - paulo

I mean, I realized I get my first name back from the bank, and it’s not what I need.

  • tried to group? something like that: SELECT MAX(data) as data, nome FROM teste WHERE id = 1 group by id

  • yes, and brings everyone with ID 1

  • Do it the other way around, first get all the entries with id = 1 and then see which one is the biggest

3 answers

2

You can solve everything with just one query:

SELECT data, nome FROM teste WHERE id = '1' order by data desc limit 1;

0

You can solve this by first sorting the query by date then another query limiting to 1.

CREATE TABLE `teste` (
  `id` int(11) NOT NULL,
  `data` datetime DEFAULT NULL,
  `nome` varchar(25) DEFAULT NULL
) 

INSERT INTO testeVALUES('2','2004-10-10','cesar'),('5','2008-10-10','joana'),
('4','2002-10-10','maria'),('1','2017-01-04','paulo'),('1','2017-12-12','paulo'),
('4','2000-10-10','maria'),('5','2017-10-10','joana');


SELECT
    aux.*
FROM(
SELECT 
    data as `data`, 
    nome 
FROM teste WHERE id = '1'
ORDER BY `data` DESC
) AS aux
LIMIT 1
;

inserir a descrição da imagem aqui

  • No function query: MSG mysql ( each derived table must have its own alias )

  • put an alias -> the result ..... and displays the same result 1 - 12/12/2017 - cesar

  • Oops, sorry, some things were missing there, I’ll edit, I had left the MAX without want, then the result was the same.

  • @Thiagolopez, if you want to test it now!

  • There was no change :/

  • Guess a DESC after ORDER BY data, there

  • Nothing friend, keeps returning the correct date, however the name is the first of the table !!

  • Honestly, I entered the same data in the table that you passed, ran the query and the result came right

  • here is no problem !

  • In fact, now that I’ve got it right .... The first taela record is coming, nor is it getting the biggest date !!

  • someone and makes a test please .... there is no way a query has two co-prorts on different pcs.

Show 6 more comments

0


After much research I found the solution to my problem and I will pass on the solution !!

SELECT * FROM test WHERE data = ( SELECT max(data) FROM test WHERE id = 1 )

Thank you all for your cooperation.

Browser other questions tagged

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