动态,使用MySQL中的where子句中的案例语句
我被卡住了一个动态,其中案例语句中的子句。
我需要的
当我使用它时,
SELECT col1,col2,col3
FROM Recharge r INNER join ft f ON f.date=r.date
WHERE f.Date LIKE
CASE WHEN f.Date BETWEEN (last_day(curdate() - interval 1 month) + interval 1 day) AND last_day(curdate())
THEN f.Date ELSE f.date between subdate(curdate(),interval 1 month) and (last_day(curdate() - interval 1 month)) END
ORDER BY f.id desc;
语法是错误的,而是“ 2022-04%”。
SELECT col1,col2,col3
FROM Recharge r INNER join ft f ON f.date=r.date
WHERE f.Date LIKE
CASE WHEN f.Date BETWEEN (last_day(curdate() - interval 1 month) + interval 1 day) AND last_day(curdate())
THEN f.Date ELSE '2022-04%' END
ORDER BY f.id desc;
这是正确的,但我想动态更改。我该怎么做。 我的意思是 当我运行查询时,包括1-4-2022到30-4-2022的日期。 快照我的数据库包括昨天的数据,我有问题。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
案例语句只能返回一个值,而不能返回范围。如果我正确地理解,只是开始日期的变化,因此我们只需要确定案例语句中的开始日期即可。如果结束日期也更改,我们将需要第二个案例语句。
但是,看起来一个简单的测试将返回相同的结果,
重新阅读您的解释,我认为您真的想要
db> gt; fiddle 在这里
A case statement can only return a value and not a range. If I understand rightly it is only the start date which changes so we only need to decide the start date in the case statement. If the end date also changes we will need a second case statement.
However it looks like a simple test will return the same results
Re-reading your explanation I think that you really want
db<>fiddle here