为什么sequelize原始查询返回与SQL查询不同的结果

时间:2020-07-22 21:41:32

标签: mysql sql node.js sequelize.js

我有sql查询

SELECT * FROM (
    SELECT dt.dt AS "dt", 
           ifnull(sum(t.dollarGross), 0) AS "gross", 
           ifnull(sum(t.quantity), 0) AS "quantity", 
           0 AS "dataset" 
    FROM (
        SELECT DATE(x.timeCreated) AS "dt", x.* 
        FROM transactions `x`) `t` 
    RIGHT JOIN datalist `dt` ON (dt.dt = t.dt) 
    WHERE (dt.dt between now() - interval 1 week and now()) 
    GROUP BY dt.dt 
    UNION (
        SELECT dt.dt AS "dt", 
               ifnull(sum(t.dollarGross), 0) AS "gross", 
               ifnull(sum(t.quantity), 0) AS "quantity", 
               1 AS "dataset" 
        FROM (
            SELECT DATE(x.timeCreated) AS "dt", x.* 
            FROM transactions `x`) `t` 
        RIGHT JOIN datalist `dt` ON (dt.dt = t.dt) 
        WHERE (dt.dt between now() - interval 2 week and now() - interval 1 week) 
        GROUP BY dt.dt)) `z` 
ORDER BY dataset ASC, dt DESC

在标准redhat mysql客户端5.5.57中返回此结果

+---------------------+-------+----------+---------+
| dt                  | gross | quantity | dataset |
+---------------------+-------+----------+---------+
| 2020-07-22 00:00:00 |     0 |        0 |       0 |
| 2020-07-21 00:00:00 |     0 |        0 |       0 |
| 2020-07-20 00:00:00 |     0 |        0 |       0 |
| 2020-07-19 00:00:00 |     0 |        0 |       0 |
| 2020-07-18 00:00:00 |     0 |        0 |       0 |
| 2020-07-17 00:00:00 |  1328 |        2 |       0 |
| 2020-07-16 00:00:00 |     0 |        0 |       0 |
| 2020-07-15 00:00:00 |     0 |        0 |       1 |
| 2020-07-14 00:00:00 |     0 |        0 |       1 |
| 2020-07-13 00:00:00 |     0 |        0 |       1 |
| 2020-07-12 00:00:00 |     0 |        0 |       1 |
| 2020-07-11 00:00:00 |     0 |        0 |       1 |
| 2020-07-10 00:00:00 |     0 |        0 |       1 |
| 2020-07-09 00:00:00 |     0 |        0 |       1 |
+---------------------+-------+----------+---------+

但是当我使用sequelize执行原始查询时,它会返回

[
  { dt: 2020-07-22T05:00:00.000Z, gross: 0, quantity: 0, dataset: 0 },
  { dt: 2020-07-21T05:00:00.000Z, gross: 0, quantity: 0, dataset: 0 },
  { dt: 2020-07-20T05:00:00.000Z, gross: 0, quantity: 0, dataset: 0 },
  { dt: 2020-07-19T05:00:00.000Z, gross: 0, quantity: 0, dataset: 0 },
  { dt: 2020-07-18T05:00:00.000Z, gross: 0, quantity: 0, dataset: 0 },
  { dt: 2020-07-17T05:00:00.000Z, gross: 0, quantity: 0, dataset: 0 },
  { dt: 2020-07-16T05:00:00.000Z, gross: 0, quantity: 0, dataset: 0 },
  { dt: 2020-07-15T05:00:00.000Z, gross: 0, quantity: 0, dataset: 1 },
  { dt: 2020-07-14T05:00:00.000Z, gross: 0, quantity: 0, dataset: 1 },
  { dt: 2020-07-13T05:00:00.000Z, gross: 0, quantity: 0, dataset: 1 },
  { dt: 2020-07-12T05:00:00.000Z, gross: 0, quantity: 0, dataset: 1 },
  { dt: 2020-07-11T05:00:00.000Z, gross: 0, quantity: 0, dataset: 1 },
  { dt: 2020-07-10T05:00:00.000Z, gross: 0, quantity: 0, dataset: 1 },
  { dt: 2020-07-09T05:00:00.000Z, gross: 0, quantity: 0, dataset: 1 }
]

代码

    static async periodicSales ({ interval, duration }) {
        let rSql = squel.select()
        .field('dt.dt', 'dt')
        .field('ifnull(sum(t.dollarGross), 0)', 'gross')
        .field('ifnull(sum(t.quantity), 0)', 'quantity')
        .from(squel.select()
            .field('DATE(x.timeCreated)', 'dt')
            .field('x.*')
            .from('transactions', 'x'), 't')
        .right_join('datalist', 'dt', 'dt.dt = t.dt')
        .group('dt.dt');
        
        let pData = makePrimaryBetweenInterval({ interval, duration, whereField: 'dt.dt' });
        let sData = makeSecondaryBetweenInterval({ interval, duration, whereField: 'dt.dt' });
        
        let pSql = rSql.clone().field('0', 'dataset', { dontQuote: true }).where(pData.where)
        let sSql = rSql.clone().field('1', 'dataset', { dontQuote: true }).where(sData.where)
        pSql.union(sSql);
        let sql = squel.select().field('*').from(pSql, 'z').order('dataset').order('dt', false);
        
        try {
            let data = await sequelize.query(sql.toString(), {
                type: sequelize.QueryTypes.SELECT
            });
            return [null, data];
        } catch (err) {
            return [err, null];
        }
    }

提示:

没有错误。

我已经检查了sql。 Sequelize和Squuel都显示相同的sql,并记录与客户端中相同的sql。

代码和sql都是从同一用户执行的。

在本地(MySQL 8.0,MySQL 5.7 Win 10 x64,两台不同的计算机)代码可以正常工作。服务器MySQL是5.5.57(Amazon Linux AMI)。原始sql在5.5.57、5.7、8上进行了测试,并返回正确的结果。

在最近两年中,相同类型的查询没有任何问题。

有什么想法可以挖掘吗?

0 个答案:

没有答案