【发布时间】:2020-03-12 07:07:51
【问题描述】:
我有两个表兴趣和subint,我想获取与每个兴趣相关的数据,所以我尝试了内连接select interest,subinterest from interest inner join subint on subint.interest_id = interest.id;,结果是
"interest" "subinterest"
"foodie" "tea"
"foodie" "biryani"
"foodie" "chocolate"
"foodie" "cookies"
"foodie" "fast-food"
"cooking" "cooking"
有什么方法可以格式化我的输出并使其像
"interest" "subinterest"
"foodie" "tea"
"biryani"
"chocolate"
"cookies"
"fast-food"
"cooking" "cooking"
是否可以通过任何 SQL 查询?或者我需要通过编码来完成。 我想要这个,因为我正在使用 node 和 knex.js 创建一个 API,并且我希望 JSON 格式正确
{
"interest": "foodie",
"subinterest": "coffee",
"subinterest": "tea",
"subinterest": "biryani"
}
【问题讨论】:
-
这不是 SQL 的目的。如何格式化结果取决于编码。只有你在 SQL 中要做的,就是按兴趣列排序。
标签: sql json postgresql knex.js