添加数据库中的无序列表?

问题描述 投票:-1回答:1

我正在尝试构建一个包含几列的字符串内容表,其中有些列包括(项目符号)无序列表。这是值列表,数组还是其他东西?这是一个示例:

|----|----------------------|----------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------|
| 1 | Transition Age Youth | • Wrap-around services • Case management • Voluntary residential services • Employment and permanent housing support | • Outreach and engagement  • Telemedicine  • Counseling and services |```

Is this as simple as using line breaks /n or /r/n in the data itself? 

I'll be using this as an open-data directory of services. I intend to have a search function where people can find all records containing keywords they specify. There's about 10 more columns I'm not showing which would help others narrow by filtering rows. 

Thank you for any insight. 

[!This photo illustrates one of several tables. Some have multiple unordered lists, this one illustrates a single list. There may be a limited list of options if that would help knowing too.[1]][1]


  [1]: https://i.stack.imgur.com/MaTmV.png
arrays postgresql html-lists
1个回答
0
投票

分隔符(或任何用文本定界符分隔)的值通常表示未规范化的数据,因此应避免将其存储在数据库中。我建议以下设计:

id | idx | desc
1  | 1   | Prevent re-institutionalization and homelessness;
1  | 2   | Transition Age Youth
1  | 3   | Wrap-around services
1  | 4   | Case management
1  | 5   | Voluntary residential services

如果要生成项目符号分隔的列表,只需使用Postgres的string_agg函数:

SELECT
    id, 
    string_agg(desc, '•' order by idx) as list
FROM your_table
GROUP BY
    id;
© www.soinside.com 2019 - 2024. All rights reserved.