I have some minor problem with an SQL query. I have to select data from multiple tables like:
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 results from t2 into an array. Because the length of the array returned is made by t2 which is the pictures table. This will return an array with 2 objects.
It is possible to return one object in the array with both pictures in it?
In MySQL, JOIN , CROSS JOIN , and INNER JOIN are syntactic equivalents (they can replace each other). In standard SQL, they are not equivalent. INNER JOIN is used with an ON clause, CROSS JOIN is used otherwise.
A NATURAL JOIN is a JOIN operation that creates an implicit join clause for you based on the common columns in the two tables being joined. Common columns are columns that have the same name in both tables. A NATURAL JOIN can be an INNER join, a LEFT OUTER join, or a RIGHT OUTER join.
When using "IN" logical operator with strings, each string should be wrapped with quotation marks. This is not necessary if all values are numeric values.
8. How many tables may be included with a join? Explanation: Join can be used for more than one table. For 'n' tables the no of join conditions required are 'n-1'.
MySQL
does not support array datatypes.
You can return a comma separated list of values instead:
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
Arrays doesn't exist in mysql. But you can use GROUP_CONCAT to return all images in 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With