【问题标题】:in operator not working with subquery in OrientDBin 运算符不使用 OrientDB 中的子查询
【发布时间】:2021-06-21 08:48:01
【问题描述】:

我在 OrientDB 中有以下 sql 查询来查找社交网络中最活跃的客户的姓名:

SELECT name
FROM Customer
Where id in (Select id, count(id) as cnt
             from (Select IN('PersonHasPost').id[0] as id
                   From Post
                   Where creationDate>= date( '2012-10-01', 'yyyy-MM-dd')
                  )
             Group by id
             Order by cnt DESC 
             limit 10
            )
GROUP BY id;

但是,此查询不返回任何结果。 当我单独运行子查询时,它确实给了我 10 个最活跃客户的 ID,以及帖子的数量,这让我觉得 in 运算符有问题。 我在这里做错了什么? 我在 OrientDB 3.0.5 上运行此查询。

【问题讨论】:

  • 查询无效,应引发错误。错误处理能力差?
  • Where id in (Select id, count(id) as cnt 正在尝试将 1 个值与 2 个值进行比较。无效的。删除 count()。
  • 这与错误处理无关,语法正确,比较结果为假,所以什么都不返回是正常的

标签: sql database select orientdb multi-model-database


【解决方案1】:

我可以立即看到一些问题。试试这个:

SELECT name
FROM Customer
Where id in (Select id
             from (Select IN('PersonHasPost').id[0] as id
                   From Post
                   Where creationDate>= date('2012-10-01', 'yyyy-MM-dd')
                  ) p
             Group by id
             Order by count(*) DESC 
             limit 10
            );

注意事项:

  • 您的子查询返回两列,但 IN 只需要 1 列。
  • 某些数据库需要派生表的表别名,因此这也可能是个问题。
  • 外部GROUP BYSELECT 不匹配。我不确定你真正想要什么,但我认为没有必要聚合或重复消除。

【讨论】:

  • 我使用 group by 来计算用户的帖子数,或者换句话说,第二个子查询返回单个 id 的次数。我尝试运行上面的查询,但现在出现以下错误:com.orientechnologies.orient.core.sql.OCommandSQLParsingException: Error parsing query: select id FROM Customer Where id in (Select id from (Select IN('PersonH​​asPost') .id[0] as id From Post Where creationDate >= date('2012-10-01', 'yyyy-MM-dd')) p Group by id Order by count(*) DESC limit 10) ^ 遇到""在第 1 行第 31 列。期待以下之一:
猜你喜欢
  • 2015-01-30
  • 2016-04-09
  • 2020-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-06
相关资源
最近更新 更多