【问题标题】:about the array in condition [duplicate]关于条件中的数组[重复]
【发布时间】:2022-01-06 12:29:34
【问题描述】:

表 1: https://i.stack.imgur.com/XVyYf.png

表2: https://i.stack.imgur.com/762uM.png

期待结果: https://i.stack.imgur.com/lNssy.png

如果数组[R80,R01,R02,R03,R04,R05...] 和 'Table2' 中有很多数据, 所以我想在数组中找到'Table2'数据 结果必须“日期”是新的(DESC)/限制 1

示例:

点赞select * from tabel2 where ID = 'R80' DESC Date Limit1

但我想获得所有物品[R80,R01,R02,R03,R04,R05...]

点赞select * from tabel2 where in [R80,R01,R02,R03,R04,R05...] DESC Date Limit1??

请帮忙~谢谢

最好不要使用“循环,声明,@”

Please dont answer 
select * from tabel2 where ID = 'R80'...;
select * from tabel2 where ID = 'R01'...;
select * from tabel2 where ID = 'R02'...;
                   ...

【问题讨论】:

  • 您对此有何疑问?有什么不适用于WHERE ID in [a,b,c]
  • 您使用的是哪种 DBMS 产品? “SQL”只是所有关系数据库使用的一种查询语言,而不是特定数据库产品的名称(并非所有支持数组)。请为您使用的数据库产品添加tagWhy should I tag my DBMS

标签: sql oracle greatest-n-per-group


【解决方案1】:

您似乎正在根据最新日期查找所有 ID 的前 1 行。使用行号

SELECT * FROM (
    --mark 1st row as 1 for every ID
    SELECT *,row_number() over(partition by ID order by date desc) RN from table2
    where id in ('R80','R01','R02','R03','R04','R05') 
) INNER_QUERY 
WHERE RN = 1

【讨论】:

  • 是的,我根据最新日期查找所有 ID,非常感谢~
猜你喜欢
  • 1970-01-01
  • 2020-05-15
  • 1970-01-01
  • 2021-07-29
  • 1970-01-01
  • 2019-06-15
  • 2021-09-09
  • 2011-12-29
  • 1970-01-01
相关资源
最近更新 更多