所以,基本上我需要达到这个效果,有什么建议吗?
JOIN ((CASE WHEN (SELECT COUNT(e.order_id) FROM entry ee WHERE ee.order_id = o.id) > 1
THEN (SELECT * FROM entry e WHERE e.user_id = 3 )
ELSE (SELECT * FROM entry e WHERE e.user_id = 3 AND e.draw_id = (SELECT dd.id
FROM draw dd
WHERE dd.draw_date_time > CURRENT_TIMESTAMP(0)
ORDER BY dd.draw_date_time ASC
LIMIT 1))
END) e ON e.order_id = o.id
答案 0 :(得分:1)
不是你怎么做的。将条件放在on
子句中。如果我正确地遵循逻辑:
JOIN
(SELECT e.*, ROW_NUMBER() OVER (PARTITION BY ee.order_id) as cnt
FROM entry e
) e
ON e.order_id = o.id and
e.user_id = 3 and
(e.cnt <= 1 or
e.draw_id = (SELECT dd.id
FROM draw dd
WHERE dd.draw_date_time > CURRENT_TIMESTAMP(0)
ORDER BY dd.draw_date_time ASC
LIMIT 1
)
我怀疑这可以进一步简化。如果没有更大的查询的上下文和逻辑应该做什么的解释,很难提出建议。你可能想问另一个问题。