在 MYSQL 中应用双重条件
Applying dual conditions in case when in MYSQL
(问题已编辑)我的 table 有 id,status,date,sequence
情况:获取满足的ids:
date
是max,不超过今天的和 status
是A
- 如果超过 1 个具有相同
date
的状态,则仅当它具有最大值时才获取 ID sequence
我在 dbVisulizer
中写 MYSQL
。
编辑:尝试查询:
select id, max(date), max(sequence) from
table
where
table.date<=now() and status='A'
order by date desc,sequence desc
听起来我只是在问直接问题而没有自己尝试任何事情,但是对于这种情况我完全被卡住了,我尝试了 case when
但无法真正取得任何好处, 任何起点都会被应用。
select id from
table
where
table.date<now() and table.status='A'
order by date desc,sequence desc
limit 1;
请自行检查是否有错误
select id, max(date), max(sequence) from
table
where
table.date<=now() and status='A'
order by date desc,sequence desc
group_by id;
这应该会给你想要的结果。将 table 和字段名称调整为您的 table 和字段。
(问题已编辑)我的 table 有 id,status,date,sequence
情况:获取满足的ids:
date
是max,不超过今天的和status
是A- 如果超过 1 个具有相同
date
的状态,则仅当它具有最大值时才获取 IDsequence
我在 dbVisulizer
中写 MYSQL
。
编辑:尝试查询:
select id, max(date), max(sequence) from
table
where
table.date<=now() and status='A'
order by date desc,sequence desc
听起来我只是在问直接问题而没有自己尝试任何事情,但是对于这种情况我完全被卡住了,我尝试了 case when
但无法真正取得任何好处, 任何起点都会被应用。
select id from
table
where
table.date<now() and table.status='A'
order by date desc,sequence desc
limit 1;
请自行检查是否有错误
select id, max(date), max(sequence) from
table
where
table.date<=now() and status='A'
order by date desc,sequence desc
group_by id;
这应该会给你想要的结果。将 table 和字段名称调整为您的 table 和字段。