select - Cassandra 错误 - 仅当分区键受 EQ 或 IN 限制时才支持 Order By

标签 select cassandra sql-order-by cql

这是我正在创建的表,该表包含有关上次参加 Mundial 杯的球员的信息。

CREATE TABLE players ( 
      group text, equipt text, number int, position text, name text,
      day int, month int, year int, 
      club text, liga text, capitan text,
PRIMARY key (name, day, month, year));

执行以下查询时:

Obtain 5 names from the oldest players that were captain of the selection team



这是我的查询:
SELECT name FROM players WHERE captain='YES' ORDER BY year DESC LIMIT 5;

我收到此错误:

Order By only supported when partition key is restricted by EQ or IN



我认为是我创建的表有问题,但我不知道如何解决。

谢谢。

最佳答案

您尝试运行的查询的表定义不正确。

您已经定义了一个包含分区键“name”、聚集列“day”、“month”、“year”和各种其他列的表。

在 Cassandra 中,所有 SELECT 查询都必须使用 EQ 或 IN 指定分区键。您可以使用您在 SQL 中使用的等式和不等式运算符来包含部分或全部聚类列。

聚类列必须按它们定义的顺序包含。 ORDER BY 子句只能包含尚未由 EQ 指定的聚类列,同样按照它们定义的顺序。

例如,您可以编写查询

select * from players where name = 'fiticida' and day < 5 order by month desc;

或者
select * from players where name = 'fiticida' and day = 10 and month > 2 order by month asc;

但不是
select * from players where name = 'fiticida' and year = 2017;

不包括“日”或“月”

并不是
select * from players where name = 'fiticida' and day = 5 order by year desc;

其中不包括“月”。

Here是关于 SELECT 查询的官方文档。

为了满足您的查询,该表需要
  • 由 EQ 或 IN 指定的分区键:“captain”将起作用
  • 使用最左侧聚类列的 ORDER BY 子句:将“年”放在主键定义中“月”和“日”的左侧
  • 关于select - Cassandra 错误 - 仅当分区键受 EQ 或 IN 限制时才支持 Order By,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46921455/

    相关文章:

    sql - 在 SQL 中要求列包含每个指定值的更好方法

    mysql - SELECT inside INSERT 语句不起作用

    hadoop - HBase 按值排序

    apache-spark - 将数据湖与已删除的记录同步

    php - 如何添加对数据进行排序的按钮或链接?

    mysql - 选择匹配最多的字符

    mysql 计数 group_concat 中的项目

    cassandra - 提交日志同步周期

    php - 使用 SQL 连接对大多数记录进行排序

    php - 如何以 2013-02-27 00 :00:00 so I can use it with ORDER BY? 形式转换日期