Imdadullah Babu
Imdadullah Babu

Reputation: 49

Don't show multiple records in SQL Query

I have a table called test_series_records Where users can give their test, They can give the test multiple times. Now I want to show all the records that given at the last. In example:

Table has record like this:

Table Image with the data

As you can see the 506900 and 506901 id's are the same user. I want the result with the 506901 id because it's created last time. Similarly I want all the records has last id or Max id but not multple entries. How can I achive that?

I have tried this:

SELECT TRUNCATE id, user_id, test_id, total_questions, correct_answers, wrong_answers, skipped_answers, test_time, time_taken FROM `test_series_records` WHERE `test_id` = 62;

Upvotes: 0

Views: 257

Answers (1)

Sachin Sarola
Sachin Sarola

Reputation: 1041

I hope this will help you the inner query will get the each user_id and with it I call another column id with Max function that will return max id for each user id and I have made temp table of both the column and then I will join that temp table with same table to get all column of related latest id

SELECT * FROM test_series_records AS tsr
INNER JOIN (SELECT user_id, MAX(id) AS id FROM test_series_records GROUP BY 
user_id) AS temp on temp.id = tsr.id;

Upvotes: 1

Related Questions