0

我有两个表具有以下类型的数据:

articleid, userid, date purchased

userid, provstate, country

我想生成一个如下所示的报告:

Count   Article Id    Date          Country    Prov/State
  3          1        April 2013     Canada       Ontatio
  1          1        April 2013     Canada       PEI
  4          1        April 2013     USA          Maine

数据应按国家和省按日期升序和字母顺序排序,并按 articleid 升序分组。

所以它基本上会显示关于文章 1 的所有信息,然后是文章 2,等等。

有人有什么好的建议吗?

我的尝试围绕着这个基本查询:

SELECT COUNT( articleid ) , articleid, DATE_FORMAT( date_purchased,  "%M %Y" ) , country, provstate
FROM story_history, user_info
WHERE story_history.userid = user_info.userid

和 futz 与 ORDER BY AND GROUP BY

4

1 回答 1

1

按您未汇总的字段分组(articleid、DATE_FORMAT( date_purchased,"%M %Y" ) 、country、provstate )并按您要排序的字段排序(date 、country、Province)

SELECT COUNT( articleid ) , articleid, DATE_FORMAT( date_purchased,  "%M %Y" ) `date` , country, provstate
FROM story_history, user_info
WHERE story_history.userid = user_info.userid
group by articleid, DATE_FORMAT( date_purchased,  "%M %Y" ) , country, provstate
order by `date` , country  Province
于 2013-04-23T18:12:33.637 回答