Hi all,
having trouble selecting from multiple tables and getting unique multiple values from them combined.
I have three tables, call them "Products", "Users" and "Files":
#1 Products:
Fields: ID (key), make, model, price, seller (reference to "Users" ID), photo1 (reference to "Files" ID), photo2 (-"-), photo3 (-"-)
#2 Users:
Fields: ID (key), username, fullname, email, telephone, address, city, zip
#3 Files:
Fields: ID (key), productID (reference to "Products" ID), filename, mimetype, width, height
I'm trying to select data from the "Users" and "Files" tables which would match with one unique row from the "Products" table. One Product can have up to three product-related photos so I would like to match one product with possibly up to those three available files. I've truncated the query a bit to display on the relevant fields.
My query is as follows:
SELECT m.id AS mid,
p.make,
p.model,
p.photo1,
p.photo2,
p.photo3,
u.id AS uid,
u.username,
f.id AS fid,
f.productid,
f.filename,
FROM products AS p
users AS u
files AS f
WHERE m.seller = u.id
AND f.productid = m.id
AND m.id=1234;
This query, given that the product 1234 has three files, returns three rows which contains all the unique files related to the product, but all other columns are identical - ie. I'm getting the same product three times over.
+-----+-------+-------+--------+--------+--------+--------+-----+----------+-----+-------------------+-----------+
| mid | make | model | photo1 | photo2 | photo3 | seller | uid | username | fid | filename | productid |
+-----+-------+-------+--------+--------+--------+--------+-----+----------+-----+-------------------+-----------+
| 244 | mfct1 | chair | 125 | 126 | 127 | 12 | 43 | user123 | 125 | 244-43_photo1.jpg | 244 |
| 244 | mfct2 | chair | 125 | 126 | 127 | 12 | 43 | user123 | 126 | 244-43_photo2.jpg | 244 |
| 244 | mfct2 | chair | 125 | 126 | 127 | 12 | 43 | user123 | 127 | 244-43_photo3.jpg | 244 |
+-----+-------+-------+--------+--------+--------+--------+-----+----------+-----+-------------------+-----------+
I'm not that proficient in MySQL - can I get a result set with one row containing the product but three filenames (probably need to be aliased?). Can I do it with a JOIN or UNION select ? Ie I would like to get something which has the columns "filename1", "filename2" and "filename3" which all would be related to one unique product.
Any pointers would be more than welcome! Thanks!