id(pk) | user_id | liked_user_id | timestamp
id(pk) | user_id | name | age | gender | timestamp
id(pk) | user_id_1 | user_id_2 | proposed_time | agreed | timestamp
SELECT
info.user_id,
info.name,
info.age,
info.gender,
DATE_FORMAT(likes.time, '%c/%e/%y') AS matchedTime,
t.proposed_time AS proposedTime,
t.agreed
FROM info
INNER JOIN likes
ON info.user_id = likes.liked_user_id
AND likes.user_id = ?
LEFT JOIN (SELECT
user_id_1,
user_id_2,
MAX(agreed) AS agreed,
MAX(DATE_FORMAT(proposed_time, '%b %d, %Y @ %h:%i %p')) AS proposed_time
FROM matches_appointment_times
WHERE proposed_time > NOW()
GROUP BY user_id_1,
user_id_2) t
ON (info.user_id = t.user_id_1 OR info.user_id = t.user_id_2)
AND (t.user_id_1 = ? OR t.user_id_2 = ?)
WHERE likes.liked_user_id IN (SELECT
user_id
FROM likes
WHERE likes.liked_user_id = ?)
ORDER BY
CASE
WHEN t.proposed_time IS NOT NULL AND t.agreed = 1 THEN t.proposed_time
END DESC,
CASE
WHEN t.proposed_time IS NOT NULL AND t.agreed = 0 THEN t.proposed_time
END DESC,
CASE
WHEN t.proposed_time IS NULL THEN likes.time
END DESC
答案 0 :(得分:0)
Please use Distinct in Subquery:
SELECT
info.user_id,
info.name,
info.age,
info.gender,
DATE_FORMAT(likes.time, '%c/%e/%y') AS matchedTime,
t.proposed_time AS proposedTime,
t.agreed
FROM info
INNER JOIN likes
ON info.user_id = likes.liked_user_id
AND likes.user_id = ?
LEFT JOIN (SELECT
Distinct
user_id_1,
user_id_2,
MAX(agreed) AS agreed,
MAX(DATE_FORMAT(proposed_time, '%b %d, %Y @ %h:%i %p')) AS proposed_time
FROM matches_appointment_times
WHERE proposed_time > NOW()
GROUP BY user_id_1,
user_id_2) t
ON (info.user_id = t.user_id_1 OR info.user_id = t.user_id_2)
AND (t.user_id_1 = ? OR t.user_id_2 = ?)
WHERE likes.liked_user_id IN (SELECT
user_id
FROM likes
WHERE likes.liked_user_id = ?)
ORDER BY
CASE
WHEN t.proposed_time IS NOT NULL AND t.agreed = 1 THEN t.proposed_time
END DESC,
CASE
WHEN t.proposed_time IS NOT NULL AND t.agreed = 0 THEN t.proposed_time
END DESC,
CASE
WHEN t.proposed_time IS NULL THEN likes.time
END DESC