Sequelize GROUP BY仅在主表上聚合,而不是在联接

时间:2016-05-20 06:46:37

标签: mysql node.js join sequelize.js

我有五张表格USER_ATTRIBQUESTION_MAINREPLY_MAINCATEGORY_MAINQSTN_CATG彼此相关,如下所示:

m.QUESTION_MAIN.belongsTo(m.USER_ATTRIB, { foreignKey: 'POSTER_I', targetKey: 'USER_I'});
m.QUESTION_MAIN.hasMany(m.REPLY_MAIN, { foreignKey: 'QSTN_I' });
m.QUESTION_MAIN.belongsToMany(m.CATEGORY_MAIN, { through: m.QSTN_CATG, foreignKey: 'QSTN_I' });
m.QUESTION_MAIN.hasMany(m.QSTN_CATG, { foreignKey: 'QSTN_I' });

我想在QUESTION_MAIN上运行查询以获取有关问题的详细信息。其中一个细节是问题的答复数量,可以通过

查询

SELECT COUNT(REPLY_MAINs.QSTN_I) GROUP BY QSTN_I;

我要运行的合并查询是:

SELECT `QUESTION_MAIN`.*
       ,`USER_ATTRIB`.`USERATTRIB_ID` AS `USER_ATTRIB.USERATTRIB_ID`
       ,`USER_ATTRIB`.`USER_NAME` AS `USER_ATTRIB.USER_NAME`
       ,`QSTN_CATGs`.`QSTN_CATG_ID` AS `QSTN_CATGs.QSTN_CATG_ID`,
       ,`QSTN_CATGs`.`CATG_I` AS `QSTN_CATGs.QSTN_CATG_I`
       ,`REPLY_MAINs`.`REPLY_ID` AS `REPLY_MAINs.REPLY_ID`
       , COUNT(`REPLY_MAINs`.`QSTN_I`) AS `REPLY_MAINs.REPLY_COUNT`
FROM (
        SELECT `QUESTION_MAIN`.`QUESTION_ID`
               , ( 6371 * acos( cos( radians(13.0508629) ) * cos( radians( QSTN_LOC_LAT ) ) * cos( radians( QSTN_LOC_LONG ) - radians(77.6092108) ) + sin( radians(13.0508629) ) * sin( radians( QSTN_LOC_LAT ) ) ) ) AS `DISTANCE`
        FROM `QUESTION_MAIN` AS `QUESTION_MAIN` 
        WHERE (
                SELECT `QSTN_I` 
                FROM `QSTN_CATG` AS `QSTN_CATG` 
                WHERE (`QSTN_CATG`.`QSTN_I` = `QUESTION_MAIN`.`QUESTION_ID`) LIMIT 1 
                ) IS NOT NULL  
        HAVING `DISTANCE` < 5 
        ORDER BY `QUESTION_MAIN`.`CREATED` DESC LIMIT 3
        ) AS `QUESTION_MAIN` 
LEFT OUTER JOIN `USER_ATTRIB` AS `USER_ATTRIB` ON `QUESTION_MAIN`.`POSTER_I` = `USER_ATTRIB`.`USER_I` 
INNER JOIN `QSTN_CATG` AS `QSTN_CATGs` ON `QUESTION_MAIN`.`QUESTION_ID` = `QSTN_CATGs`.`QSTN_I` 
LEFT OUTER JOIN `REPLY_MAIN` AS `REPLY_MAINs` ON `QUESTION_MAIN`.`QUESTION_ID` = `REPLY_MAINs`.`QSTN_I` 
              AND `REPLY_MAINs`.`REPLY_STATUS` = 200
GROUP BY `QUESTION_ID` 
ORDER BY `QUESTION_MAIN`.`CREATED` DESC;

这是制作该查询的Sequelize:

QUESTION_MAIN.findAll({
attributes:['QUESTION_ID', 'POSTER_I',
  ['( 6371 * acos( ' 
                  + 'cos( radians('+qstnFeedRequest.qstnLocLat+') ) ' 
                  + '* cos( radians( QSTN_LOC_LAT ) ) '
                  + '* cos( radians( QSTN_LOC_LONG ) - radians('+ qstnFeedRequest.qstnLocLong+') ) '
                  + '+ sin( radians('+qstnFeedRequest.qstnLocLat+') ) '
                  + '* sin( radians( QSTN_LOC_LAT ) ) ) '
  + ')', 'DISTANCE'
  ]
],
include: [
  { model: USER_ATTRIB, 
    attributes:['USER_NAME']
  },
  { model: QSTN_CATG, 
    attributes: [['CATG_I', 'QSTN_CATG_I']],
    where: qstnCatgWhereClause
  },
  { model: REPLY_MAIN, 
    attributes: [[sequelize.fn('COUNT', sequelize.col('REPLY_MAINs.QSTN_I')), 'REPLY_COUNT']], 
    where: {REPLY_STATUS: 200},
    required: false
  }
],
having:{ 'DISTANCE' : {$lt: 5} },
where: whereClause,
group: ['QUESTION_ID'],
limit: qstnFeedRequest.limit
})

问题是GROUP BY子句正在里面内部查询,而不是整个连接:

SELECT `QUESTION_MAIN`.*,
   ...
FROM (
    SELECT `QUESTION_MAIN`.`QUESTION_ID`,
    ...  
    HAVING `DISTANCE` < 5 
    GROUP BY `QUESTION_ID` -- This should go outside
    ORDER BY `QUESTION_MAIN`.`CREATED` DESC LIMIT 3
    ) AS `QUESTION_MAIN` 
LEFT OUTER JOIN `USER_ATTRIB` ...
ORDER BY `QUESTION_MAIN`.`CREATED` DESC;

这导致计数错误聚合。无论我尝试什么,我都无法从内部查询中获取GROUP BY子句。

如何单独对整个连接进行分组而不是单独使用主表?

1 个答案:

答案 0 :(得分:3)

在探索了一半的网络后,我终于找到了解决方案。

如在link is in the comment的线程中所述,在连接外部进行限制的1:M查询效率很低。因此,Sequelize对1:1和1:M关系分别进行查询,因为 separate: true属性是在1:M表的include语句中设置的。

即使在此之后,也存在一些问题:

如果表的连接列未包含在属性中,则代码会中断。

Sequelize也会在内部表上应用外部having子句。为了防止这种情况,我在包含中添加了一个真正的having语句。

这是我修改后的最终Sequelize:

QUESTION_MAIN.findAll({
attributes:['QUESTION_ID', 'POSTER_I',
  ['( 6371 * acos( ' 
                  + 'cos( radians('+qstnFeedRequest.qstnLocLat+') ) ' 
                  + '* cos( radians( QSTN_LOC_LAT ) ) '
                  + '* cos( radians( QSTN_LOC_LONG ) - radians('+ qstnFeedRequest.qstnLocLong+') ) '
                  + '+ sin( radians('+qstnFeedRequest.qstnLocLat+') ) '
                  + '* sin( radians( QSTN_LOC_LAT ) ) ) '
  + ')', 'DISTANCE'
  ]
],
include: [
  { model: USER_ATTRIB, 
    attributes:['USER_NAME']
  },
  { model: QSTN_CATG, 
    attributes: [['CATG_I', 'QSTN_CATG_I']],
    where: qstnCatgWhereClause
  },
  { model: REPLY_MAIN, //this is the 1:M table
    attributes: ['QSTN_I', [sequelize.fn('COUNT', sequelize.col('REPLY_MAIN.QSTN_I')), 'REPLY_COUNT']], 
    //QSTN_I is the column joining QUESTION_MAIN and REPLY_MAIN. Not including this in the attributes throws an error 
    where: {REPLY_STATUS: 200},
    group: ['QSTN_I'], //grouping it in this query instead of the main query
    separate: true,//the culprit
    having: {'REPLY_COUNT': {$ne: null}}, //this is a dummy having clause which always returns true. This is added to stop the outer having clause being applied to the inner query
    required: false
  }
],
having:{ 'DISTANCE' : {$lt: 5} },
where: whereClause,
limit: qstnFeedRequest.limit
})

希望这可以节省某人的2天时间&#39;时间