I have two tables:
ID | user | info
------------------
1 1 data1
2 1 data2
3 2 data3
table1_id | array_info
------------------------
1 a
1 b
2 a
3 d
3 g
3 m
How can I efficiently return the rows associated with a user such that I get info and an array of that ID's associated array_info?
For instance, a query like "select ID, info from table1 where user = 1" but would also return [a,b] from table2 so that the result would be something like [1, data1, [a, b]] with the first row and [2, data2, [a]] with the second row. Similarly, "...where user = 2" should return something like [3, data3, [d, g, m]]. I don't think MySQL returns things quite like this, but perhaps PHP can smooth over the input of a left join? It seems wasteful to replicate all the data in info if MySQL returns a separate row for each array_info, but I'm not sure there's another way.
You could use a GROUP_CONCAT with a JOIN and then when you iterate over the results, use explode() to convert the concatenated string to an array.
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.