【发布时间】:2021-06-12 23:22:43
【问题描述】:
我希望选择带有逗号分隔值的类别名称的帖子,如下所示:
帖子表
| id | post_name | post_content |
|---|---|---|
| 1 | first post | first post content |
| 2 | second post | second post content |
| 3 | third post | third post content |
关系表
| relation_id | post_id | cat_id |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 1 | 4 |
| 3 | 1 | 5 |
| 4 | 2 | 4 |
| 5 | 2 | 5 |
| 6 | 3 | 2 |
| 7 | 3 | 3 |
分类表
| cat_id | cat_name | cat_slug | cat_desc |
|---|---|---|---|
| 1 | Cat One | catone | lorem ipsum one |
| 2 | Cat Two | cattwo | lorem ipsum two |
| 3 | Cat Three | catthree | lorem ipsum Three |
| 4 | Cat Four | catfour | lorem ipsum Four |
| 5 | Cat Five | catfive | lorem ipsum Five |
结果:
| id | post_name | post_content | cat_slug | cat_name |
|---|---|---|---|---|
| 1 | first post | first post content | catone,catfour,catfive | Cat One, Cat Four, Cat Five |
| 2 | second post | second post content | catfour,catfive | Cat Four, Cat Five |
| 3 | third post | third post content | cattwo,catthree | Cat Two, Cat Three |
我尝试了组联系,但没有成功,它可以处理多个查询和 foreach 循环,但我想知道是否有可能只用一个查询来实现。
【问题讨论】:
-
认真考虑处理应用代码中数据显示的问题
标签: mysql