Lachesis Lachesis - 3 months ago 15
MySQL Question

SQL: SELECT FROM multiple tables

I have 10 tables communicating between them with parent keys an I am trying to pull out all the information that I need.

This is the query that I am using right now:

SELECT a.date_in, a.date_out, b.name, b.phone, b.birthdate,
b.country, b.hotel, b.room_nr, b.passport_nr, c.email,
d.size, e.name, GROUP_CONCAT(g.service), GROUP_CONCAT(h.service)
, GROUP_CONCAT(i.time), GROUP_CONCAT(j.location)
FROM reservation a, rider b, user c,
bike_size d, bike e, services_reservation f, services g,
bike_shipping h, bike_shipping_reservation i
, bike_shipping_location j WHERE a.rider_id = b.id
AND b.user_id = c.id AND a.bike_size_id = d.id AND
d.bike_id = e.id AND a.id = f.reservation_id AND
f.services_id = g.id
AND h.id = i.bike_shipping_id AND a.id = i.reservation_id
AND i.bike_shipping_location_id = j.id
AND a.id = 80


The problem is that all the columns where I use GROUP_CONCAT function, get duplicated many times. For example:

GROUP_CONCAT(g.service) column
Road SPD,Road SPD,Bike Helmet,Bike Helmet,TT Bar,TT Bar,Garmin Edge
810,Garmin Edge 810


should be

Road SPD,Bike Helmet,TT Bar,Garmin Edge 810


Any ideas how I can fix it? is my database scheme good?

SQLFiddle:
http://sqlfiddle.com/#!9/8bc033/33

Answer

Use DISTINCT in your GROUP_CONCAT statements

SELECT a.date_in, a.date_out, b.name, b.phone, b.birthdate,
b.country, b.hotel, b.room_nr, b.passport_nr, c.email,
d.size, e.name, GROUP_CONCAT(DISTINCT g.service), GROUP_CONCAT(DISTINCT h.service)
, GROUP_CONCAT(DISTINCT i.time), GROUP_CONCAT(DISTINCT j.location)
FROM reservation a, rider b, user c,
bike_size d, bike e, services_reservation f, services g,
bike_shipping h, bike_shipping_reservation i
, bike_shipping_location j WHERE a.rider_id = b.id
AND b.user_id = c.id AND a.bike_size_id = d.id AND
d.bike_id = e.id AND a.id = f.reservation_id AND
f.services_id = g.id
AND h.id = i.bike_shipping_id AND a.id = i.reservation_id
AND i.bike_shipping_location_id = j.id
AND a.id = 80