for loop type query in mysql

I have a table ('metrics') which has fields "code" and "date".
There are a number of codes and a number of dates for each code.

I am using MySQL 5.1 and am intending on doing this a select query only.

basically, I want a distinct list of codes with the latest date for each one.

I thought it would be easy, but after procedures, sub queries, max, distinct, and so on I still can't seem to get it to work.
I've searched the interwebs and there seem to be loads of solutions, but none that actually work.

Thanks for any help you can give me,


Who is Participating?
monkey3Author Commented:

ok, it was just about getting the query in the correct order...

select max(date), code from metrics
group by code

thanks all the same :)
First                               Last
Code | Date                    Nr. | Date
1111   1                          1      02.02.2009
1112   2                          2      02.03.2009

SELECT t2.Date FROM First t1 INNER JOIN Last t2 ON t1.Date = t2.Nr WHERE t1.Date = 1
- This will select Date from table "Last" where Nr = 1 -> 02.02.2009

tho i dont know what u need exactly

if u want select codes for last 10 inputs
SELECT t1.Code, t2.Date FROM First t1 INNER JOIN Last t2 ON t1.Date = t2.Nr ORDER BY DESC LIMIT BY 10


monkey3Author Commented:
ok, I didn't explain very well then.

so the table is as so:

Code | Date
1111   20050101
1112   20050102
1111   20060413
1112   20060419
1113   20070101

And I want the following data:
Code | Date
1111   20060413
1112   20060419
1113   20070101

Simply the last date that each code occurred.

Thanks again for your help,
Network Scalability - Handle Complex Environments

Monitor your entire network from a single platform. Free 30 Day Trial Now!

try use :

SELECT * FROM tbl_name ORDER BY desc LIMIT 3

this will query last 3 biggest  dates (ascending/descending)

if u use some server side code (php for example)

<input type="text" name="number" class="" size="20"> -- HTML

$number = $_POST['number'];
SELECT * FROM tbl_name ORDER BY desc LIMIT $number
monkey3Author Commented:
Unfortunately there may well be thousands of 'codes' and we will definitely not know how many there are.

I need to do something like:

select distinct (code) from metrics
for each record
   select code, max(date)

I'm sure I need to use Cursors, but I just can't get it to work.
DISTINCT clause can be considered as a special case of GROUP BY. For example, the following two queries are equivalen

monkey3Author Commented:
yes it can.

So do you understand what I need now?
This has to be an sql statement that will return every code and the date they last occurred.

Well, my english is little bad so i apologize for this first :P

if i understand :

FROM metrics
WHERE date BETWEEN '4/13/06' AND '1/1/07';

but if u want return just LAST date use ORDER BY date DESC and LIMIT 1
monkey3Author Commented:
lol, no problem :)

But I think you missed the point.

For each code:
Get the last date it occurred.

I think the code you have suggested is too simplified.
It will need to be run at many points during the day so cannot contain static dates or codes.
hm, yes i think i missed the point tho i dont know exactly what u need ..

how u add data ..what u read and why u read those data..

u tell me it cannot contain static dates then its about some shop or something ..when u read with bar code reader and sell some article code and date are updated..or something similar

sorry for my eng
monkey3Author Commented:
This table contains tens of thousands of codes and the dates those codes occurred.
(Think of it as a table of error codes if you like)

The data is added by a separate process which I cannot control.

I need to give a list of the codes that occurred and the most recent occurrence of those codes.

The query cannot contain static dates.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.