Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想先显示订阅尚未结束的用户,然后显示其他没有 subs_end_datetime 的用户。我已经尝试使用以下 sql 但没有返回任何内容。
$sql = "select * from users order by case when subs_end_datetime <= CURRENT_DATE() then 0 else 1, id";
你在寻找这样的东西吗?
SELECT * FROM users ORDER BY (COALESCE(subs_end_datetime, 0) <= CURDATE()), id
这是SQLFiddle演示
根据您的评论
SELECT *, subs_end_datetime <= CURDATE() aa FROM users ORDER BY (COALESCE(subs_end_datetime, 0) <= CURDATE()), subs_end_datetime DESC
这样我们会显示订阅尚未结束的用户。我们在查询中使用了>=insted of <=,以及尚未设置任何 ends_subscription_datetime 的用户 ( null)。
>=
<=
null
SELECT * FROM users HAVING (coalesce(subs_end_datetime, 0) >= current_date() OR ((coalesce(subs_end_datetime,0)) = 0)) ORDER BY subs_end_datetime ASC
示例: http ://sqlfiddle.com/#!2/a255c/18