Jump to content

Retrieve last record of each group in a table


abdur_afzal

Recommended Posts

Hi,

 

I want to retrieve last record of each group. I am using the following qury and it is giving me the last record of the table not each group.

 

$sql="SELECT * FROM $tbl_name ORDER by a_datetime DESC LIMIT 1";

$result=mysql_query($sql);

<?php

while($rows=mysql_fetch_array($result)){ // Start looping table row

?>

Any help will be appreciated.

 

Thanks

Link to comment
Share on other sites

Table forum_question

CREATE TABLE `forum_question` (

`id` int(4) NOT NULL auto_increment,

`topic` varchar(255) NOT NULL default '',

`detail` longtext NOT NULL,

`name` varchar(65) NOT NULL default '',

`email` varchar(65) NOT NULL default '',

`datetime` varchar(25) NOT NULL default '',

`view` int(4) NOT NULL default '0',

`reply` int(4) NOT NULL default '0',

PRIMARY KEY (`id`)

) TYPE=MyISAM AUTO_INCREMENT=1 ;

 

Table forum_answer

CREATE TABLE `forum_answer` (

`question_id` int(4) NOT NULL default '0',

`a_id` int(4) NOT NULL default '0',

`a_name` varchar(65) NOT NULL default '',

`a_email` varchar(65) NOT NULL default '',

`a_answer` longtext NOT NULL,

`a_datetime` varchar(25) NOT NULL default '',

KEY `a_id` (`a_id`)

) TYPE=MyISAM;

 

What i basically want is the last answer of each question from the "forum_answer" table.

 

Thanks very much.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.