Jump to content

Last 5 entries with php and mysql


wipe

Recommended Posts

I honestly don't know if there is a more efficient way than this (if there is, [b]Barand[/b] would know :) ), but here's my solution:

let's say you have 3 tables called

table1
table2
table3

and in each table is date column. it doesn't matter if they are all named the same or not, as long as they are the same data type.  but let's just say for the sake of clarity they are all named different:

date1
date2
date3

and we want to choose the 5 most recent dates out of all 3 tables overall, right? okay so here we go:

[b](SELECT date1 FROM table1) UNION (SELECT date2 FROM table2) UNION (SELECT date3 FROM table3) ORDER BY date1 DESC LIMIT 5[/b]

okay so here's an explanation of what this does: this runs 3 seperate queries all at once.  Then it merges all the data into one column.  The column is named the same name as what is selected in the first query so since that is date1, the result will return your list with the column named date1.  That is why we are ordering by date1 at the end. 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.