I'm having a problem with a MySQL query. I've been following: How to select the most recent set of dated records from a mysql table and what I'm trying to do is select the most recient record and sort it by the sales_total field. So first I have to sort by max(timestamp) then sort again by sales_total. I've modified the solution offered above as follows:
$query = "(SELECT * FROM (SELECT * FROM table_name WHERE year =\'2013\' AND category=\'Network\' ORDER BY timestamp DESC) as t1 GROUP BY name) ORDER BY sales_total DESC";
$result = mysql_query($query) or die("Query failed");
This runs perfectly in PHPMyadmin, however If I try and execute the same query in a PHP script I get a Query Failed error. Anyone know why the above seems to work in PHPMyadmin but not in PHP?
My PHP query is as follows:
$query = "(SELECT * FROM (SELECT * FROM table_name WHERE category=\'Network\' ORDER BY timestamp DESC) as t1 GROUP BY name) ORDER BY sales_total DESC";