在MySQL中没有得到案例语句的结果
我试图在MySQL中执行以下查询,期望如下
- 如表中不存在记录,
- 如果存在记录,则返回当前时间戳,请比较开始日期和结束日期和结束日期的值,并返回适当的值。
但是,对于没有记录的第一个执行,以下查询没有返回当前的时间戳值。这里有什么问题?
SELECT
CASE WHEN start_dt < end_dt THEN end_dt
WHEN start_dt >= end_dt THEN start_dt
ELSE CURRENT_TIMESTAMP() END AS start_dt
FROM test_master_table WHERE
schema_nm='test_db' and table_nm='test_table';
I am trying to execute the below query in MYSQL, The expectation is as below
- If no record exists in table, return current timestamp
- if record exists, compare the values of start date and end date and return appropriate values.
However, for the first execution, where there are no records, the below query is not returning the current timestamp value. What could be wrong here?
SELECT
CASE WHEN start_dt < end_dt THEN end_dt
WHEN start_dt >= end_dt THEN start_dt
ELSE CURRENT_TIMESTAMP() END AS start_dt
FROM test_master_table WHERE
schema_nm='test_db' and table_nm='test_table';
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
else
在case
中不会神奇地使一行出现:没有行表示没有行。要始终行,将查询(大部分)用作子查询并应用Cocece:
The
else
in thecase
doesn't magically make a row appear: No rows means no rows.To always get a row, use (most of) your query as a subquery and apply coalesce:
您也可以从带有一行的临时表中加入查询:
You can also left join your query from an ad hoc table with a single row: