JAVAで、MySQLのSQL文を作っています。
customer 顧客マスタ:ユニークに顧客レコードが存在
customer_reservation 予約マスタ:同一顧客でも複数のレコードが過去未来に存在。
顧客でユニークに(customer.customer_id)、
customer_reservation.reservation_date + " " + customer_reservation.reservation_starttime が
最大のレコードを抽出したいのですが、
下記では、テーブル中で、一番最初に見つけたレコードを抽出したいのですが、抽出0件です。
ご指摘か所が多いかもしれませんが、ご指摘頂ければ幸いです。
※customer_reservation.reservation_dateのフォーマットは、YYYY-MM-DDです。
※customer_reservation.reservation_starttimeのフォーマットは、"HH:MM"です。
SELECT customer_reservation., customer., user.name FROM customer
JOIN (customer_reservation JOIN user ON customer_reservation.staff_id = user.user_id) ON customer_reservation.customer_id = customer.customer_id
WHERE
(customer_reservation.id = 79)
AND (customer.id= 79)
AND (customer_reservation.delete_flag = false)
AND (customer.delete_flag = false)
AND (customer_reservation.customer_id, customer_reservation.reservation_date) IN ( SELECT customer_reservation.customer_id, max(DATE_FORMAT(customer_reservation.reservation_date + ' ' + customer_reservation.reservation_starttime, '%H-%k-%I %r:%T')) FROM customer_reservation WHERE (customer_reservation.delete_flag = false) GROUP BY customer_reservation.customer_id )
GROUP BY customer.customer_id
ORDER BY customer_reservation.reservation_date DESC
回答1件
あなたの回答
tips
プレビュー