forbes — 2013-03-21T11:05:13-04:00 — #1
I've written a query that is the equivalent of asking someone to go find a sock but to also return anything else they found along with it.
I have bookings, each of which have a one-to-many relationship with one to [n] data objects. While I'm asking the database to find one of those data objects, I actually need it to retrieve any other data objects it might find. Alas, it's not working that way and it's only returning what it's finding.
I've emboldened the part of the query which is the source of my angst.
I'm sure this is a very common problem, but I can't articulate the problem to even search for a solution!
As always, any advice would be warmly received.
DATE_FORMAT(bookings.creation_date, '%a %D %b %y') AS creation_date,
DATE_FORMAT(bookings.modification_date, '%a %D %b %y') AS modification_date,
DATE_FORMAT(bookings_attendees.stay_date, '%a %D %b %y') AS stay_date,
venues.venue_id AS venue_id,
venues.name AS venue,
MAX(bookings_history.modification_date) AS booking_history
(bookings_history.booking_id = bookings.booking_id)
), '%a %D %b %y, %H:%i') AS booking_history,
COALESCE(GROUP_CONCAT(DISTINCT bookings_clients_options_data.value SEPARATOR 0x1D), 'NULL') AS clients_options
(bookings.booking_id = bookings_attendees.booking_id)
(bookings.client_id = '3')
(bookings.status = 'confirmed')
(bookings.mode = 'public')
(bookings.booking_id = bookings_clients.booking_id)
(clients.client_id = bookings_clients.client_id)
(clients.client_id = '1')
(venues.venue_id = bookings.venue_id)
(bookings_clients_options.client_id = '3')
(bookings_clients_options.bookings_client_option_id = bookings_clients_options_data.bookings_client_option_id)
(bookings_clients_options_data.booking_attendee_id = bookings_attendees.booking_attendee_id)
(bookings_clients_options_data.bookings_client_option_id = '4')[/B]
GROUP BY bookings.booking_id
ORDER BY bookings_attendees.stay_date
r937 — 2013-03-21T12:31:54-04:00 — #2
this is a bit nebulous... please rephrase it in terms of all seven tables in your query
think about this for a minute and then tell me you wouldn't want sql to work any other way
forbes — 2013-03-21T13:07:20-04:00 — #3
- bookings and bookings_attendees both contain the actual booking data (split because it supports more than one booking per person made in one session);
- clients isn't necessarily required here, but is necessary for when other types of user are using the system, and is required to determine which client they belong to;
- venues is the venue to which the booking relates;
- bookings_clients_options contains the custom fields which the client can use to capture data beyond the mandatory fields required for the booking (name, email, venue, stay date, number of nights et cetera);
- bookings_clients_options_data is for the actual data objects themselves.
As for your question .. think about my opening sentence for a minute and then tell me if you realised I was being sarcastic.
r937 — 2013-03-21T13:25:07-04:00 — #4
if you were being sarcastic then i am sorry because i don't see from your query why if it's not finding what it wasn't asked to find that this is a problem
forbes — 2013-03-21T13:31:30-04:00 — #5
No, it is finding the "sock", but it's not returning the "sofa" it was behind and the "carpet" they both sat on. It's an example I used to the client to explain how weird the query needs to be.
Okay, so booking 54,321 has three data objects; 123, 124, and 125. I run the query to see if data object 125 is the sock, and if so, return 123 "sofa" and 124 "carpet", too. Or, more specifically, any data objects it can find. Presently, it's only returning what it finds, which is the sock.
r937 — 2013-03-21T14:36:16-04:00 — #6
why does one of your joins specify client_id 1 and another client_id 3?
forbes — 2013-03-22T05:40:35-04:00 — #7
Because there's a parent-child relationship to the clients. In this instance, the company the system belongs to is the client and their customer "client" is a child of that.
I wouldn't be concerned with the client side of things; it's a piece of code that's present in about 70% of the queries throughout the system, and is working.
I'll confess, it's not the ideal structure (I intend changing this at a later date), but it was a design forced on me by circumstance at the time.
forbes — 2013-03-26T05:33:46-04:00 — #8
Over the weekend and yesterday, I've tried several types of subquery; in the SELECT and as part of the query body itself, but with no luck. Any ideas?
r937 — 2013-03-26T07:34:55-04:00 — #9
i never did understand what you were trying to do