gpt4 book ai didi

sql - 如何像这样显示 SQL 结果

转载 作者:行者123 更新时间:2023-12-04 05:40:20 26 4
gpt4 key购买 nike

我想加入表 Documents(id, name) 和 Tags(tagId, tagname, docId(FK))。但是结果会返回很多多余的行,因为一个文档可以有很多标签:

Document_name |Tag
----------------------
document01 tag01
document01 tag02
document02 tag01

我试图解决的所有问题是:
document_name |Tags
----------------------------
document01 | tag01, tag02
document02 | tag01

或者可以是这样的:
Document_name |Tag_1   |Tag_2  |Tag_...
---------------------------------------
document01 tag01 |tag02
document02 tag01

任何人都知道如何实现这种情况?非常感谢! (我试图在这个网站上找到其他答案,但我不知道在这种情况下搜索合适的关键字)

最佳答案

如果您使用的是 MySQL,您可以这样做:

select document_name, group_concat(tag SEPARATOR ', ')
from Documents
group by document_name

关于sql - 如何像这样显示 SQL 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11338653/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com