假設一個表有 id 和 date 列,有沒有辦法對每個 id 應用不同的日期過濾器?
我能想到的一種方法是添加一個填充有 if 梯子的標志列
如果 id=A 且日期>date1 則為 1
elseif id=B and date>date2 then 1
elseif id=C and date>date3 then 1
否則 0
然后選擇 flag=1 的行
有沒有更好的辦法?
uj5u.com熱心網友回復:
只需使用常規的 AND/OR 結構。例如:
where (id=A and date>date1)
or (id=B and date>date2)
or (id=C and date>date3)
uj5u.com熱心網友回復:
- 您可以使用
case when - 稍后您可以過濾諸如
date_and_id_filter = 1或之類的值date_and_id_filter in (1,2,3)。基本上,你可以玩它
with main as (
select *,
case when id = 'a' and date>date1 then 1
when id = 'b' and date>date2 then 1
when id= 'c' and date>date3 then 1
else 0 end as date_and_id_filter
from <table_name>
)
select * from main where date_and_id_filter = 1
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/529580.html
標籤:mysqlsql
上一篇:如何使mysql```UNIX_TIMESTAMP()```回傳自定義時間戳?
下一篇:讀取基于位置的txt
