[英]Mysql do a query if case 1, do another query if case 2
I need a query for SCD2 in MySQL using:我需要使用以下命令查询 MySQL 中的 SCD2:
DATA STRUCTURE: id, data, start_date, end_date
数据结构: id, data, start_date, end_date
if the record_id exist:如果 record_id 存在:
-update the record's end_date, -更新记录的结束日期,
-and create a new record with the new data - 并使用新数据创建新记录
else:别的:
-insert a new record. - 插入一条新记录。
Can I use MySQL CASE to this?我可以使用 MySQL CASE 吗?
You dont need an end-date to store historic data;您不需要结束日期来存储历史数据; Whenever you want to store a new address, you simply add it with the start-date.每当您想存储新地址时,只需将其添加到开始日期即可。 You retrieve history by SELECT * FROM table ORDER BY start-date Or just the last address by SELECT * FROM table ORDER BY start-date DESC LIMIT 1您通过 SELECT * FROM table ORDER BY start-date 或仅通过 SELECT * FROM table ORDER BY start-date DESC LIMIT 1 检索历史记录
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.