0

不知道如何做到这一点,但我正在尝试从 MYSQL DB 中获取所有拖欠多年的包裹。现在,我可以使用以下查询计算这些年来我有多少拖欠:

SELECT 
    parcel, COUNT(*), due
FROM 
    `deliquent_property`
WHERE year IN ('2013', '2012', '2011', '2010', '2009') AND
      CAST(replace(replace(ifnull(due,0),',',''),'$','') AS decimal(10,2)) > 0
GROUP BY parcel
HAVING COUNT(*) > 2;

所以我得到这样的东西

'XXX-XXX-XXX1'    '3'  '167.00'
'XXX-XXX-XXX2'    '4'  '190'

其中金额是数据库中该包裹的最后一条记录。问题是我真正想要的是显示每年逾期金额的包裹列表,如下所示(跳过那些没有至少 3 次拖欠的包裹):

'XXX-XXX-XXX1'    '2013'  '1267.78'
'XXX-XXX-XXX1'    '2012'  '1000.38'
'XXX-XXX-XXX1'    '2011'  '167.00'
'XXX-XXX-XXX2'    '2013'  '1000.00'
'XXX-XXX-XXX2'    '2012'  '500.00'
'XXX-XXX-XXX2'    '2011'  '100.00'
'XXX-XXX-XXX2'    '2010'  '190.00'

我已经完成了一半,但我迷失了最后一部分。

4

1 回答 1

1

您可以将表格重新加入您的列表。这是一种方法:

select dp.*
from delinquent_property dp join
     (SELECT parcel, COUNT(*)
      FROM delinquent_property
      WHERE year IN ('2013', '2012', '2011', '2010', '2009') AND
           CAST(replace(replace(ifnull(due,0),',',''),'$','') AS decimal(10,2)) > 0
     GROUP BY parcel
     HAVING COUNT(*) > 2
    ) p3
    on dp.parcel = p3.parcel
where CAST(replace(replace(ifnull(due,0),',',''),'$','') AS decimal(10,2)) > 0
order by parcel;
于 2013-08-24T18:58:54.153 回答