We are wanting to build a recent activity table in our website which will act as a timeline for recent orders. The current table id | order date | shipping date | declined date 1 11-01-11 19-01-11 2 13-01-11 14-01-11 3 15-01-11 16-01-11 4 15-01-11 15-01-11 The required output should be (sorted by most recent date date) 1 19-01-11 (shipped) 3 16-01-11 (shipped) 4 15-01-11 (shipped) 2 14-01-11 (declined) One way I have though to do this is add another field in the table 'updated date' and then simply sort by this. I am just wondering if there is a more efficient way.
You don't need to add a field to the table, you just create it on the fly in the query: SELECT TableName.id, IF([declined date]>[shipping date],[declined date],[shipping date]) AS UpdatedDate FROM TableName ORDER BY IF([declined date]>[shipping date],[declined date],[shipping date]) DESC; PHP: Be sure to replace TableName with the actual name of your table.
writing a query is more efficient than creating a VIEW or JOINING TABLE.. querying is faster process..