0

我已经查看了许多解决这个问题的假设,但我仍然无法让它发挥作用。

SELECT Year FROM (SELECT DISTINCT YEAR(detDate1) AS Year 
FROM growl_details WHERE publish = 'y' AND pubNews = 'y' 
UNION 
SELECT DISTINCT YEAR(detDate1) AS Year FROM growl_pl_details
WHERE publish = 'y' AND pubNews = 'y') AS t ORDER BY 'Year' ASC

我有两个表,growl_details 和 growl_pl_details,我试图让它们只显示存在信息的年份,然后这将用于创建一个表,其中包含指向与该年份相关的数据页面的链接。

显然,我希望按顺序显示年份,但上面的代码似乎是单独订购它们,并且它们相互依存..

有任何想法吗?

4

1 回答 1

0

您的查询(格式化以便我可以阅读):

SELECT Year
FROM (SELECT DISTINCT YEAR(detDate1) AS Year
      FROM growl_details
      WHERE publish = 'y' AND pubNews = 'y'
      UNION
      SELECT DISTINCT YEAR(detDate1) AS Year
      FROM growl_pl_details
      WHERE publish = 'y' AND pubNews = 'y'
     ) AS t
ORDER BY 'Year' ASC;

问题是由于该order by条款。尝试将单引号更改为反引号:

ORDER BY `Year` ASC;

单引号用于字符串常量。反引号包围事物的名称。因此,您的原始查询是按常数排序的——这会产生任意排序。

于 2013-08-10T16:53:02.153 回答