anujphp Posted December 8, 2008 Share Posted December 8, 2008 Hello I have a table with 4 fields like table name=profile name,address,category,date the category column have 7 different value and i am required to fetch the most recent entry in each category THANK YOU IN ADVANCE Link to comment https://forums.phpfreaks.com/topic/136030-retrive-from-mysql-by-searching-on-date-and-another-column/ Share on other sites More sharing options...
chronister Posted December 8, 2008 Share Posted December 8, 2008 <?php $query1 = "SELECT distinct('category') as cat FROM profile"; // should get unique category names $result = mysql_query($query1); while($row = mysql_fetch_object) { $cat = $row->cat; $query = "SELECT * FROM profile WHERE category = '$cat' ORDER BY date LIMIT 1"; $result = mysql_query($query); while($row2 = mysql_fetch_object($result)) { // do what you want with the data received here... $name = $row2->name; $address = $row2->address; $category = $row2->category; $date = $row2->date; } } ?> I think that should work. I am sure there is a better way ,but it is 3 am and my brain is tired... so thats the best I got right now. Nate Link to comment https://forums.phpfreaks.com/topic/136030-retrive-from-mysql-by-searching-on-date-and-another-column/#findComment-709259 Share on other sites More sharing options...
anujphp Posted December 10, 2008 Author Share Posted December 10, 2008 Thank you chronister/Nate your code works fine,i was lookign for something liek that,THANK YOU ONCE AGAIN Link to comment https://forums.phpfreaks.com/topic/136030-retrive-from-mysql-by-searching-on-date-and-another-column/#findComment-711181 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.