View Single Post

   
  #4 (permalink)  
Old 02-28-2008, 07:14 AM
alfred Wallace
 
Posts: n/a
Default Re: Help for quite complex query.

_andrea.l wrote:
> Hallo,
> I have a quite complex query:
> I have a table
>
> create tabel event
> [...]
> y INT
> date TIMESTAMP
> group INT
> [...]
>
> I'd like to know the value of y of the row which have the MAX(date) in
> 'group'.
>
> something like that
> SELECT y FROM event WHERE date is the max of (GROUP BY group)
>
> How can I DO?
> THnakyou in advance,
> Andrea.
>
>

first you should rename your field group group1 ( because group is a
reserved name)
alter table event change 'group' 'group1' int(8);

You could not obtain "y" witch matches max(date) in one query:
select max(date),group1,y from event group by group1
-> returns a random y among group1.

the right way needs php.
PHP code:
<?
include('yourconnection.php');

//
$qu1 = "select max(date),group1 from event group by group1";
$res = mysql_query( $qu );

while( $row = mysql_fetch_array( $res )){

$qu2 = "select y,date,group1 from event where
date=".$row['max(date)']." and group1 = ".$row['group1'];

$res2 = mysql_query( $qu2 );

$row2 = mysql_fetch_array( $res2 );
//( or while $row2 = ... because $res2
// may return more than 1 result )

// print results:
echo "<pre>";
print_r( $row2 );
echo "</pre><hr>";
}
?>
Reply With Quote