I have this sql query:
SELECT
"bookings"."customerID",
"bookings"."arrivalDate",
"bookings"."leavingDate",
"bookings"."bookingID",
"bookings"."pickup",
"bookings"."dropoff",
"bookings"."locationID",
"locations"."locationName",
"customers"."customerName"
FROM
"bookings"
LEFT JOIN "customers" ON
"bookings"."customerID" = "customers"."customerID"
LEFT JOIN "locations" ON
"bookings"."locationID" = "locations"."locationID"
WHERE (
"bookings"."arrivalDate" BETWEEN '2014-11-15' AND '2014-11-22'
)
What i'm hoping to get is all the bookings for the week ahead. Sorted by oldest first. Just cant get it to work!
SELECT
b.customerID,
b.arrivalDate,
b.leavingDate,
b.bookingID,
b.pickup,
b.dropoff,
b.locationID,
l.locationName,
c.customerName
FROM
bookings b
LEFT JOIN customers c
ON b.customerID = c.customerID
LEFT JOIN locations l
ON b.locationID = l.locationID
WHERE
b.arrivalDate > curdate()
AND b.arrivalDate <= date_add( curdate(), INTERVAL 7 DAY )
order by
b.leavingDate DESC
I updated to utilize aliases vs long table name references. Also, the where clause to make use of the current date and 7 days forward to get the week ahead so it is not hard-referenced. You could additionally adjust in case you wanted the query always to be based on a Sat or Sun of a given week.
As for the sorting by the "oldest" first... oldest in what way... just change the order by clause. I am GUESSING your intent was on the date leaving out... So by that column DESCENDING would put the oldest LEAVING item at the top of the list.