gpt4 book ai didi

MySQL/hive 查询数组中每一项的一个结果

转载 作者:行者123 更新时间:2023-11-29 17:54:07 25 4
gpt4 key购买 nike

我有一个 id 列表,我有兴趣在 hive 中选择数据。对于其中的每一个 id,都有一个或多个实体存储在名为 books 的表中,这些实体在 global_parent_id 列中具有该 id。我想知道是否可以在传递 id 列表的位置编写一个查询,对于列表中的每个 id,仅从 books 中获取一个将 id 作为其 global_parent_id 的条目。我正在尝试这个 Hive 查询(也将接受 MySQL 建议),但它只返回一个结果,而我想为每个父图书 ID 返回一个子图书 ID。提前致谢。

select distinct(book_id) from books where global_parent_id in (‘23434’, 23425’, ‘22322’) limit 1;

最佳答案

limit 1 即使多行满足条件也只会返回 1 条记录。在 Hive 中使用 row_number 函数。(根据您的规则更改 row_number 中的 order by)

select global_parent_id,child_book_id
from (select global_parent_id,child_book_id,
row_number() over(partition by global_parent_id order by child_book_id) as rnum
from books
where global_parent_id in ('23434', '23425', '22322')
) t
where rnum=1

关于MySQL/hive 查询数组中每一项的一个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49016345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com