简体   繁体   中英

SQL query many-to-many realtionship

So I have 4 tables:

vehicles:

+----+-------------+------------------------+
| id |    title    |      description       |
+----+-------------+------------------------+
|  1 | Lorem ipsum | amet coscutor lorem et |
+----+-------------+------------------------+

prducers:

+----+-----------------+-----------------+----------+
| id |      name       |     website     | location |
+----+-----------------+-----------------+----------+
|  1 | Porsche Zentrum | www.example.com | {json}   |
+----+-----------------+-----------------+----------+

vehicle_images:

+------------+----------+
| vehicle_id | image_id |
+------------+----------+
|          1 |        1 |
+------------+----------+

images:

+----+-------+-----+----------------------------------+
| id | title | alt |               url                |
+----+-------+-----+----------------------------------+
|  1 | Foo   | Bar | https://example.com/imgs/img.jpg |
+----+-------+-----+----------------------------------+

vehicles has a many-to-many relationship with images . What I need as result, is a list of all vehicles, the producer-details for each vehicle and all images for each vehicle.

All I get by now, is a list of all vehicles with their corresponding dealers, but each vehicle exists as often as it has images:

+----+-------+---------------+---------+-----------+-----------+
| id | title |  description  | dealer  | img_title |  img_url  |
+----+-------+---------------+---------+-----------+-----------+
|  1 | Car 1 | Description 1 | Porsche | img 1     | img-url-1 |
|  1 | Car 1 | Description 1 | Audi    | img 2     | img-url-2 |
|  2 | Car 2 | Description 2 | Audi    | img 3     | img-url-3 |
|  2 | Car 2 | Description 2 | VW      | img 4     | img-url-4 |
+----+-------+---------------+---------+-----------+-----------+

And finally my query:

SELECT 
v.id, v.title, v.description,
p.name AS dealer,
i.title AS img_title, i.url AS img_url
FROM vehicles v
LEFT JOIN producers p on p.id = v.producer_id
LEFT JOIN vehicle_images vi on vi.vehicle_id = v.id
LEFT JOIN images i ON vi.image_id = i.id;

I can't use GROUP BY id because of the following error-message:

Error Code: 1055. Expression #9 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'projektarbeit.i.title' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

Of course I could deactivate only_full_group_by in my MySQL-settings, but I think this would not be the best solution.

You can use subquery to get necessary result, for example:

SELECT vehicles.id,
       vehicles.title,
       vehicles.description,
       producers.name AS dealer,
       images.title AS img_title,
       images.url AS img_url
FROM vehicles
LEFT JOIN images ON images.image_id =
  (SELECT MAX(image_id)
   FROM vehicle_images WHERE vehicle_id = vehicles.id)
LEFT JOIN producers ON producers.id = vehicles.producer_id

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM