[英]SQL/MYSQL : Select all column in a junction table
Hi guys i have some question how can i retrieve column data in a junction table properly. 大家好,我有一个问题,我如何才能正确检索联结表中的列数据。 for example i have one blog in junction table with many keywords, how can i get the blog including the keywords attached to it. 例如,我在联结表中有一个博客,其中包含许多关键字,我如何才能获得包含附加关键字的博客。
junction table : 连接表:
*junction_table*
|id| blogID | keywordID|
|0 | B43 | k12 |
|0 | B43 | k13 |
let assume that 'B43' blog title is "Hello World!" 假设“ B43”博客标题为“ Hello World!”。 and 'K12' keyword is 'fun' and 'k31' is 'exciting'. 而“ K12”关键字为“ fun”,而“ k31”关键字为“令人兴奋”。 i want to get data as: 我想获取数据为:
{title: 'Hello World' : keywords: { "fun", "exciting" }
how can i achieve that or is it even possible. 我如何才能实现这一目标,甚至有可能实现? thank you 谢谢
Use GROUP_CONCAT
to group the Keyword data 使用GROUP_CONCAT
对关键字数据进行分组
SELECT blogID, GROUP_CONCAT(keywordID) FROM junction_table
If you want the result as like this {title: 'Hello World' : keywords: { "fun", "exciting" }
如果您想要这样的结果{title: 'Hello World' : keywords: { "fun", "exciting" }
You can do below 你可以在下面做
SELECT blogID as `title`, GROUP_CONCAT(keywordID) as `keywords` FROM junction_table
After that, using PHP json_encode(array_here)
it will generate as string
之后,使用PHP json_encode(array_here)
将其生成为string
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.