0

我试图从我的表中获取不同的值

    let records = db
    .select("*")
    .from("user_technical_skill")
    .distinct('technical_skill_id')

例如,在user_technical_skill我的表中


[{
"id": "84ed9c04-b1d3-4e69-b2d2-c569ad94545f",
"user_id": "5dfbf2cc-38f9-4388-a077-11480e62d893",
"technical_skill_id": "111",
"created_at": "2021-04-11T15:31:39.552Z",
"updated_at": "2021-04-11T15:31:39.552Z"
},
{
"id": "4b0fcdd6-cbab-4fdf-ada6-0154c7956630",
"user_id": "a3b91e2a-5d7e-4528-b496-3a0807299db7",
"technical_skill_id": "111",
"created_at": "2021-04-11T15:48:49.145Z",
"updated_at": "2021-04-11T15:48:49.145Z"
}]

Technical_skill_id 为 11 的两列

但它不起作用。我再次得到查询中的两列我该如何解决这个问题?

4

1 回答 1

0

如果您需要所有列,您可以使用 group by 而不是 distinct()。

let records = db
    .select("*")
    .from("user_technical_skill")
    .groupBy('technical_skill_id')

否则,如果您在结果中只需要 user_technical_skill 您可以使用

let records = db.from("user_technical_skill").distinct('technical_skill_id')

您不需要 select("*") 因为 distinct 就像一个选择替代品。

参考:

于 2021-04-11T16:39:58.730 回答