Moving MySQL JOIN to an array

I have a little problem with the SQL query. I need to select data from several tables, for example:

offers

| id | offer | info | 1 | City break | information 

Pictures :

 | id | id_offer | picture_name | title | 1 | 1 | bucharest.jpg | Bucharest | 2 | 1 | london.jpg | London 

sql query :

 SELECT offers.* as t1, pictures.* as t2 FROM offers JOIN t2 ON t1.id=t2.id_offer WHERE t1.id = '1' 

The code is much larger, but I don't understand how to wrap the results from t2 into an array. Since the length of the returned array is done using t2, which is a table of images. This will return an array with two objects.

Is it possible to return one object to an array with both images in it?

+4
source share
2 answers

MySQL does not support array types of arrays.

Instead, you can return a comma separated list of values:

 SELECT o.*, GROUP_CONCAT(picture_name ORDER BY p.id) FROM offers o JOIN pictures p ON p.id_offer = o.id GROUP BY o.id 
+16
source

Arrays do not exist in mysql. But you can use GROUP_CONCAT to return all images to a comma separated list

 SELECT offers.*, GROUP_CONCAT(t2.picture_name) AS pictures FROM offers AS t1 JOIN pictures AS t2 ON t1.id=t2.id_offer WHERE t1.id = '1' GROUP BY t1.id 
+7
source

Source: https://habr.com/ru/post/1314072/


All Articles