MySQL

时间:2018-03-19 23:29:45

标签: mysql

我无法查询正确的日期范围。我的查询似乎没有考虑post_date的Where子句> (提供日期)。

            SELECT `code`,
                `description`,
                SUM( IF( month = 3 && year = 2018, monthly_quantity_total, 0 ) ) AS monthlyqt,
                SUM( IF( month = 3 && year = 2018, monthly_price_total, 0 ) ) AS monthlypt,
                SUM( monthly_quantity_total ) AS yearlyqt,
                SUM( monthly_price_total ) AS yearlypt
            FROM (
                SELECT `invoices_items`.`code`,
                    `invoices_items`.`description`,
                    SUM( invoices_items.discounted_price * invoices_items.quantity_supplied ) AS monthly_price_total,
                    SUM( invoices_items.quantity_supplied ) AS monthly_quantity_total,
                    YEAR( invoices_items.datetime_created ) AS year,
                    MONTH( invoices_items.datetime_created ) AS month
                FROM `invoices_items`
                JOIN `invoices` ON `invoices`.`id` = `invoices_items`.`invoice_id`
                WHERE `invoices`.`is_finalised` = 1
                AND `invoices`.`post_date` > 2018-02-28                 
                AND `invoices_items`.`type` = 1                 
                GROUP BY `year`, `month`, `invoices_items`.`code`

                UNION ALL

                SELECT `credit_notes_items`.`code`,
                    `credit_notes_items`.`description`,
                    SUM( credit_notes_items.discounted_price * credit_notes_items.quantity_supplied * -1 ) AS monthly_price_total,
                    SUM( credit_notes_items.quantity_supplied ) AS monthly_quantity_total,
                    YEAR( credit_notes_items.datetime_created ) AS year,
                    MONTH( credit_notes_items.datetime_created ) AS month
                FROM `credit_notes_items`
                JOIN `credit_notes` ON `credit_notes`.`id` = `credit_notes_items`.`credit_note_id`
                WHERE `credit_notes`.`is_finalised` = 1
                AND `credit_notes`.`post_date` > 2018-02-28
                AND `credit_notes_items`.`type` = 1
                GROUP BY `year`, `month`, `credit_notes_items`.`code`
            ) AS sub
            GROUP BY code;

这里基本上有4个表被查询。 invoicesinvoices_itemscredit_notescredit_notes_items

表1 - 发票

id    post_date    is_finalised
1     2018-01-01   1
2     2018-02-01   1
3     2018-03-01   1

表2 - invoices_items

id    invoice_id    code    description    discounted_price    quantity_total    type
1     1             TEST-01 Test product   9.99                1                 1
2     1             TEST-01 Test product   9.99                2                 1
3     2             TEST-01 Test product   9.99                5                 1
4     3             TEST-01 Test product   9.99                5                 1

我在上面给出了一些示例行。从上面的表1和表2可以看出所需的结果;

期望输出

code    description    monthyqt    monthlypt    yearlyqt    yearlypt

TEST-01 Test product   5           49.95        5           49.95

然而,我收到的输出如下;

收到输出

code    description    monthyqt    monthlypt    yearlyqt    yearlypt

TEST-01 Test product   5           49.95        13          129.87

除了我试图通过使用Where子句实现的日期范围之外,查询按预期工作。您可以看到我正在尝试过滤掉与invoices不匹配的任何行。post_date> 2018-02-28(以及credit_notespost_date> 2018-02-28)。

我不确定我在这里做错了什么,但是非常感谢任何帮助。

提前致谢。

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。基本上解析日期值的变量需要封装在单引号中。

e.g。

invoices.post_date > '2018-02-28'

这总是很简单的东西会让你失望。