0

我有以下 INNER JOIN 查询,您可以在此处的小提琴中看到它。

SELECT  b.*, c.date2
FROM    (
            SELECT a.work, a.amount, 
                   COUNT(*) totalCount, 
                   SUM(Amount) totalAmount
            FROM tableName a
            GROUP BY a.work, a.amount
        ) b
        INNER JOIN
        (
            SELECT a.work, a.amount, DATE_FORMAT(Date,'%D %M %Y') date2,
                    date
            FROM tableName a
        ) c ON b.work = c.work and b.amount=c.amount
ORDER BY b.work, b.totalCount, c.date

我想在查询中添加另一列,称为“url”,它的工作方式与日期列相同,即它为每一行返回“url”列中的不同值。您可以在此处找到包含新列的架构。我不确定如何调整查询以返回具有不同“url”值的额外列。我尝试了几件事,但无法使其正常工作。

4

2 回答 2

2

你可以试试这个...

    SELECT  b.*, c.date2, c.url
    FROM    (
            SELECT a.work, a.amount, 
                   COUNT(*) totalCount, 
                   SUM(Amount) totalAmount
            FROM tableName a
            GROUP BY a.work, a.amount
        ) b
        INNER JOIN
        (
            SELECT a.work, a.amount, DATE_FORMAT(Date,'%D %M %Y') date2,
                    date, url
            FROM tableName a
        ) c ON b.work = c.work and b.amount=c.amount
    ORDER BY b.work, b.totalCount, c.date

于 2013-02-23T11:07:21.460 回答
2

它可以写成更短的,比如

 SELECT a.work, a.amount, 
                   COUNT(*) totalCount, 
                   SUM(Amount) totalAmount, url, DATE_FORMAT(Date,'%D %M %Y')
            FROM tableName a
            GROUP BY a.work, a.amount, DATE_FORMAT(Date,'%D %M %Y')
于 2013-02-23T11:14:53.853 回答