for loop type query in mysql

Posted on 2009-05-12
Medium Priority
Last Modified: 2012-06-21
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,


Question by:monkey3
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5

Expert Comment

ID: 24362817
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



Author Comment

ID: 24363329
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,

Expert Comment

ID: 24364434
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
Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.


Author Comment

ID: 24365435
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.

Expert Comment

ID: 24365526
DISTINCT clause can be considered as a special case of GROUP BY. For example, the following two queries are equivalen

from http://dev.mysql.com/doc/refman/5.0/en/distinct-optimization.html

Author Comment

ID: 24366272
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.


Expert Comment

ID: 24367151
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

Author Comment

ID: 24372292
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.

Expert Comment

ID: 24372849
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 ..so 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

Author Comment

ID: 24373017
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.

Accepted Solution

monkey3 earned 0 total points
ID: 24373792

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 :)

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question