2013-07-26 70 views
-1

我遇到了MySQL查询的问题。 我一直在:How to select the most recent set of dated records from a mysql table和我想要做的是选择最recient记录和排序由sales_total字段。所以首先我必须按照max(timestamp)排序,然后再由sales_total进行排序。我已经修改了上面如下给出的解决方案:MySQL按时间戳排序预先排序的记录

$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"); 

这完全运行在phpMyAdmin,但如果我尝试和PHP脚本,我收到了查询失败的错误执行相同的查询。任何人都知道为什么上述似乎在PHPMyadmin,但不是在PHP中工作?


我的PHP查询如下:

$query = "(SELECT * FROM (SELECT * FROM table_name WHERE category=\'Network\' ORDER BY timestamp DESC) as t1 GROUP BY name) ORDER BY sales_total DESC"; 
+2

你能告诉我们你的PHP代码吗? –

+0

显示您的php代码 –

+3

“查询失败”确实是完整的完整错误信息? –

回答

0

在你的PHP代码,你有\'尽管使用"引号。此外,你似乎有一些超级(可能是错误)括号。因此,请尝试将您的PHP查询字符串更改为

$query = "SELECT * 
    FROM (
     SELECT * 
     FROM table_name 
     WHERE category='Network' 
     ORDER BY timestamp DESC 
    ) as t1 
    GROUP BY name 
    ORDER BY sales_total DESC"; 
+0

刚刚在我的PHP代码中显示了下一行,以显示查询失败错误的来源。 – Taffman

1

不幸的是,您正在查看的帖子是错误的。 MySQL文档明确指出group bygroup by子句中没有指定时为列选择任意值。

以下是来自here的明确引用。

MySQL extends the use of GROUP BY so that the select list can refer to nonaggregated columns not named in the GROUP BY clause. This means that the preceding query is legal in MySQL. You can use this feature to get better performance by avoiding unnecessary column sorting and grouping. However, this is useful primarily when all values in each nonaggregated column not named in the GROUP BY are the same for each group. The server is free to choose any value from each group, so unless they are the same, the values chosen are indeterminate. Furthermore, the selection of values from each group cannot be influenced by adding an ORDER BY clause. Sorting of the result set occurs after values have been chosen, and ORDER BY does not affect which values within each group the server chooses.

正确的做法(这是标准SQL)是计算适当的最大时间戳记,然后加入结果回:

SELECT t.* 
FROM table_name t join 
     (select name, year, category, max(timestamp) as maxts 
     from table_name 
     WHERE year ='2013' AND category='Network' 
     group by name, year, category 
    ) tn 
     on t.name = tn.name and 
     t.year = tn.year and t.category = tn.category and t.timestamp = tn.maxts 
ORDER BY sales_total DESC; 

编辑:

这是您的查询:

SELECT t.* 
FROM annualtable t join 
    (SELECT year, name, max(countries) 
     FROM annualtable 
     WHERE name NOT LIKE '%No Entries%' and year <> '0' 
     GROUP BY year 
     ORDER BY year 
    ) tn 
    on t.name = tn.name and t.year = tn.year t.countries = tn.countries 
ORDER BY year DESC; 

有两个问题。其中一个是在on的第三个条款之前缺少的and。第二个是你需要在子查询的group by中包含name。如果您希望order by做任何事情,您可能会遇到其他问题。请参阅文档中的上述参考。所以,你可能想要这个:

SELECT t.* 
FROM annualtable t join 
    (SELECT year, name, max(countries) 
     FROM annualtable 
     WHERE name NOT LIKE '%No Entries%' and year <> '0' 
     GROUP BY year, name 
    ) tn 
    on t.name = tn.name and t.year = tn.year and t.countries = tn.countries 
ORDER BY year DESC; 
+0

@Simon在mso.net,它修复了很多谢谢。使用'\和'来自PHP MyAdmin,使用Create PHP Code函数。非常感谢。 – Taffman

+0

感谢您的支持,您的查询也能正常工作。不确定我完全按照解释,但我会用它作为它。似乎是更安全的选择。感谢所有的人抽出时间来帮助。 – Taffman

+0

我有一个与另一个问题相似的问题。我尝试过戈登的例子,但我认为我在这里做错了什么或者我错了一个错字看看。我的查询如下所示:SELECT t。* FROM annualtable t join(SELECT year,name,max(countries)FROM annualtable WHERE name NOT LIKE'%No Entries%'and year <>'0'GROUP BY year ORDER BY year) tn on t.name = tn.name and t.year = tn.year t.countries = tn.countries ORDER BY year DESC;任何人在这里看到最新的错误? – Taffman