mysql:查询最大日期组由2列错误结果

时间:2017-05-26 08:21:21

标签: mysql

我的数据库上有这个表

**示例数据****

(tablename = sections_content)

section_name   |  report_text|countryname|policyname     |statecode| datepublished | dateupdated**
Executive Summary|...........|Algeria    |WEEE           |DZ       | 2016-05-11    |2017-02-16 
Executive Summary|...........|Algeria    |WEEE           |DZ       | 2017-02-15    |2017-02-16
Executive Summary|...........|Algeria    |Batteries      |DZ       | 2017-02-15    |2017-02-16
Executive Summary|...........|Algeria    |Packaging      |DZ       | 2017-02-15    |2017-02-16
Executive Summary|...........|South Africa|Packaging     |DZ       | 2017-02-15    |2017-02-16

我想找到每个statecode列和policyname列的最大日期发布

就像结果应该只有一条记录,即每个国家/政策的最大日期发布

因此,一个国家应该有3条政策(WEEE /电池/包装)的3条记录,如

Executive Summary|...........|Algeria   |WEEE           |DZ       |2017-02-15     |2017-02-16
Executive Summary|...........|Algeria   |Batteries      |DZ       |2017-02-15     |2017-02-16
Executive Summary|...........|Algeria   |Packaging      |DZ       |2017-02-15     |2017-02-16

我使用的mysql代码是

SELECT  t.section_name,
        t.report_text,
        t.countryname,
        t.policyname,
        t.statecode,
        t.datepublished,
        date_format(t.dateupdated,"%Y-%m-%d")as dateupdated

FROM sections_content t

INNER JOIN (
            select max(datepublished) as Maxdate,statecode,policyname
            from sections_content
            group by statecode,policyname
            ) tm

            ON t.datepublished= tm.Maxdate and t.statecode = tm.statecode and t.policyname=tm.policyname

WHERE t.section_name="Executive Summary"

但结果是错误的,有些国家没有选择所有政策

例如南非国家只挑选电池和包装但缺少WEEE(但数据库中有南非的WEEE)

所以我想请任何人检查我的代码有什么问题。 谢谢你的帮助。

2 个答案:

答案 0 :(得分:1)

问题是你为一个简单的问题做了太复杂的查询,你已经在查询中得到了答案:

试试这个:

SELECT  policyname,
        statecode,
        max(datepublished)
FROM sections_content
WHERE section_name="Executive Summary"
group by statecode,policyname

答案 1 :(得分:1)

DROP TABLE IF EXISTS sections_content;

CREATE TABLE sections_content
(Section_name VARCHAR(30) NOT NULL
,countryname  VARCHAR(30) NOT NULL 
,policyname VARCHAR(30) NOT NULL
,statecode CHAR(2) NOT NULL
,datepublished DATE NOT NULL
,dateupdated DATE NULL
,PRIMARY KEY(countryname,policyname,statecode,datepublished) -- assumed PK
);

INSERT INTO sections_content VALUES
('Executive Summary','Algeria','WEEE','DZ','2016-05-11','2017-02-16'),
('Executive Summary','Algeria','WEEE','DZ','2017-02-15','2017-02-16'),
('Executive Summary','Algeria','Batteries','DZ','2017-02-15','2017-02-16'),
('Executive Summary','Algeria','Packaging','DZ','2017-02-15','2017-02-16'),
('Executive Summary','South Africa','Packaging','DZ','2017-02-15','2017-02-16');

SELECT t.*
  FROM sections_content t
  JOIN 
     ( SELECT MAX(datepublished) Maxdate
            , statecode
            , policyname
            , countryname
         FROM sections_content
        GROUP 
           BY statecode
            , policyname
            , countryname
     ) tm
    ON t.datepublished = tm.Maxdate 
   AND t.statecode = tm.statecode 
   AND t.policyname = tm.policyname
   AND t.countryname = tm.countryname
 WHERE t.section_name = "Executive Summary";

 +-------------------+--------------+------------+-----------+---------------+-------------+
 | Section_name      | countryname  | policyname | statecode | datepublished | dateupdated |
 +-------------------+--------------+------------+-----------+---------------+-------------+
 | Executive Summary | Algeria      | Batteries  | DZ        | 2017-02-15    | 2017-02-16  |
 | Executive Summary | Algeria      | Packaging  | DZ        | 2017-02-15    | 2017-02-16  |
 | Executive Summary | South Africa | Packaging  | DZ        | 2017-02-15    | 2017-02-16  |
 | Executive Summary | Algeria      | WEEE       | DZ        | 2017-02-15    | 2017-02-16  |
 +-------------------+--------------+------------+-----------+---------------+-------------+