2017-05-26 23 views
-1

我有我的數據庫mysql的:通過2列錯誤的結果

**【參考數據****

(表名= 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 
此表查詢最大日化集團

,我想查找每個狀態碼列和保單名稱列的最大日期發佈

像結果應該只有一個記錄是m/AX每個國家datepublished政策

所以,一個國家應該有3條記錄3個政策(WEEE /電池/包裝)像

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的南非在數據庫上)

所以我想問任何人檢查我的代碼有什麼問題。 感謝您的幫助。

+0

看起來好我。見http://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple-sql-query – Strawberry

+0

report_text不是所提供的數據集中的列。而南非在提供的數據集中並不具有價值。 – Strawberry

+0

上面的@Strawberry表只是示例數據,但實際數據全部爲 – Patcharapan

回答

1

的問題是,你犯了一個太複雜的查詢爲一個簡單的問題,你已經有了你的查詢答案:

就試一下這種方法:

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

我可以看到在重新打印OP的子查詢中沒有優點。 – Strawberry

+0

也許沒有任何價值,因爲它是正確的查詢,並且不需要爲一個簡單的問題提供複雜的答案 – nacho

+0

OP想要返回表中的所有相關列。 – Strawberry

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 | 
+-------------------+--------------+------------+-----------+---------------+-------------+