【问题标题】:Using sequelize.js get the list of all blogs `group by` `tags` from the table使用 sequelize.js 从表中获取所有博客的列表 `group by` `tags`
【发布时间】:2023-02-16 20:35:11
【问题描述】:

我们如何从userblogs表中获取所有博客group bytags的列表

下面的sql查询:

select max(id) as id, max(blogdetails) as blog, tags, count (tags) as tagcount from userblogs group by tags;

到目前为止,我已经尝试过了;

 app.get('/service/listofblogs', async (req, res) => {
      try {
        const blogData = await BlogModel.findAll(
         {attributes: ['blogdetails', 'tags']},
         { group: ['tags'] }
        );
        res.status(200).json({ blogData });
      } catch (e) {
        res.status(500).json({ message: e.message });
      }
    });

【问题讨论】:

    标签: sequelize.js


    【解决方案1】:

    您可以尝试使用单个值而不是数组吗?

    const blogData = await BlogModel.findAll(
     {attributes: ['blogdetails', 'tags']},
     { group: 'tags' }
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-25
      • 2018-06-04
      • 1970-01-01
      • 2021-12-31
      • 2021-12-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多