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!" and 'K12' keyword is 'fun' and 'k31' is 'exciting'. 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
SELECT blogID, GROUP_CONCAT(keywordID) FROM junction_table
If you want the result as like this {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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.