sql - 一行中的 SQL 输出
问题描述
例如,我“select * from posts where id = 2”
它将在 1 行中输出所有“id 2”信息
规范化后,它有一个用于多对多关系的新表。
但我想知道如何输出标准化之前的结果?
或者我需要修改后端代码中的输出?
谢谢
解决方案
一种方法是group_concat()
,它将所有值放在一行中:
select p.*, group_concat(t.name) as tags
from posts p join
tag_post tp
on tp.post_id = p.id join
tag t
on tp.tag_id = t.id
group by p.id;
注意:这假设posts(id)
被声明为主键(或至少unique
),因此您可以使用select p.*
with group by
。
推荐阅读
- reactjs - 在 Gatsby 中以编程方式限制路线
- python - 迭代numpy数组的函数
- c# - 处理 SelectionChanged 和 MouseDown
- less-unix - 使用较少时如何转到第一个匹配的行
- javascript - 是否需要 setTimeout?
- javascript - 在 React 中映射从 Express 服务器获取的 JavaScript 对象
- docker - Docker Windows 主机 - 在用于 Linux 容器的 Linux VM 上安装 nVidia 驱动程序
- java - 尝试将我的 Java 测试代码与 OpenTest 一起使用
- vb.net - CefSharp SetZoomLevel 不起作用
- html - 允许 CSS Grid 随窗口垂直增长,填充剩余空间