mysql query latest entry?

Soldato
Joined
7 Jan 2007
Posts
10,607
Location
Sussex, UK
PHP:
           $query =		("SELECT * 
						FROM operators
						JOIN products
						ON operators.idoperators = products.idoperator JOIN results
						ON products.idProducts = results.idproducts
						WHERE (products.idProducts ='2'
						OR products.idProducts ='5'
						OR products.idProducts ='9'
						OR products.idProducts ='10'
						OR products.idProducts ='12'
						OR products.idProducts ='13')
						ORDER BY date  DESC
                        LIMIT 6");

The problem I have is idproduct 9 has more entries each week, which duplicates the entries, I want 1 entry per idproduct, how can I do that?

I do have an auto increment column...
 
I can't post the real output but here is a sample:


idproduct----result----date
2------------400------28/06/2012
5------------600------16/08/2102
9------------300------15/08/2012
12-----------900------14/08/2012
13-----------200------13/08/2012
9------------900------12/08/2012

The problem is it outputs two idproducts 9 and there for totally misses idproduct 10 because I need it limited to 1 per idproduct, but my query doesnt do this.
 
I have some tables, that I join together to make one big one.

For idproduct = 9 it has 3 entries a week, the rest have only 1 new entry per week.

The problem is I only want to show the latest entry for each idproduct.

I do not want to have 2 idproduct 9 rows in my query
 
PHP:
           $query =		("SELECT
						MAX(results.drawndate), 
						results.idProducts 
						FROM results 
						GROUP BY results.drawndate, results.idProducts");
					
					
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result) or die(mysql_error());



// Print out the contents of each row into a table
foreach($row as $element) 
{

echo $element;

}
mysql_close();

Outputs:

2012-01-01
2012-01-01
10
10

What is that?
 
Last edited:
Back
Top Bottom