How to join 1 table twice in the same query and save the results separately

we create a scheduler system and have a couple of situations when we try to get some notes from a table for display. We looked at other answers, and none of them correspond to this problem.

The order table contains a numerical reference to both the clientโ€™s note (if any) and the stylistโ€™s note (if any).

The notes table stores both client notes and stylists, each of which is indexed by a unique numerical index

We have our request when we just want to read in client notes:

SELECT bookings.bookingID, UNIX_TIMESTAMP(bookings.startDate) AS start_date, UNIX_TIMESTAMP(bookings.endDate) as end_date, clientDetails.firstName, clientDetails.lastName, clientDetails.phone1, clientDetails.phone2, clientDetails.email, services.name, services.serviceID, cNotes.note as client_notes, sNotes.note as stylist_note
FROM bookings 
LEFT JOIN clientDetails ON bookings.clientID = clientDetails.clientID
LEFT JOIN services ON bookings.serviceID = services.serviceID
LEFT JOIN notes ON bookings.clientNotes = notes.notesID
WHERE bookings.startDate >= '" . $start_date . "' AND  bookings.endDate <= '" . $end_date . "' AND bookings.stylistID = '" . $stylist_id . "'
ORDER BY bookings.startDate ASC;

Using this logic, we can access cient notes from an array of results in php just like: $ results_array ['note']

, , - , , $results_array ['stylist_note'].

, :

LEFT JOIN notes ON bookings.stylistNotes = notes.notesID

.

.

+3
1

:

SELECT ..., stylistnotes.note AS stylist_note
FROM ...
LEFT JOIN notes stylistnotes ON bookings.stylistNotes = stylistnotes.notesID
+3

Source: https://habr.com/ru/post/1742514/


All Articles