本文介紹了SQL從同一個id獲取多個值的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我在 SQL 中有一個表,其中有多個行用于同一 ID.
I have a table in SQL that has multiple lines for the same id.
例如:
ID event_id category_id
1 1 3
2 1 6
3 1 18
4 1 24
5 2 1
我想知道如何進行 SQL 查詢來實現(xiàn)每個 event_id
的輸出,它顯示了它所在的所有類別.
I am wondering how I would make an SQL query to achieve the output for each event_id
it shows all the categories it is in.
例如
{
"event_id": 1,
"categories": [3, 6, 18, 24]
}
謝謝
推薦答案
字符串聚合是否足夠?
select event_id,
string_agg(category_id, ',') within group (order by category_id)
from t
group by event_id;
這篇關于SQL從同一個id獲取多個值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!