简体   繁体   English

如何在服务器端设置分组查询。 我想设置查询,以便我可以使用 d3js 将其用于前端的饼图

[英]how do I set up a grouping query on the server side. I want set up the query so I can use it for a pie chart in front end, using d3js

I am using this query to count and group occupation types.我正在使用此查询来计算和分组职业类型。 I need to do this in order to create a pie chart for the client side for the front end.我需要这样做才能为前端的客户端创建饼图。 I don't think I need to input anything, I just want to get the information sorted and counted.我认为我不需要输入任何内容,我只是想对信息进行排序和统计。 User_Id is just referenced just in case I get any errors with authorization of the jwt token. User_Id 只是被引用,以防万一我在 jwt 令牌的授权中遇到任何错误。 The user_id is not referenced in the table that I am pulling information from, it's just there for authorization purposes.我从中提取信息的表中未引用 user_id,它只是用于授权目的。

router.get("/occupation/:user_id", authorization, async (req, res) => {
    try {
        console.log(req);
        const result = await pool.query(
            "SELECT occupation,COUNT(occupation) FROM resources GROUP BY occupation;",
            [req.params.user_id][req.body.occupation]

            // [req.body.json
            // ]
        );

        console.log(req.body);

        res.status(200).json({
            status: "success",
            data: {
                occupation: result.rows, //this gets the one row we need
            },
        });
    } catch (err) {
        console.error(err.message);
    }
});
router.get("/occ", authorization, async (req, res) => {
    try {
        console.log(req);
        const result = await pool.query(
            // "SELECT occupation,COUNT(occupation) FROM resources GROUP BY occupation;",

            "SELECT occupation,COUNT(occupation) as values FROM resources GROUP BY occupation"
        );

        console.log(req);
        console.log(result);
        res.status(200).json({
            status: "success",
            data: {
                occupationResults: result.rows, //this gets the one row we need
            },
        });
    } catch (err) {
        console.error(err.message);
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM