jOOQ为嵌套选择生成的SQL在MySQL / MariaDB中不起作用

时间:2018-05-02 19:39:41

标签: mysql sql scala jooq

对于经典排名问题(每位玩家最佳3个结果的总和),我使用 jOOQ DSL Scala (要嵌入)提出以下子查询如选择值并加入更复杂的查询中):

val tr1 = TOUR_RESULT.as("tr1")
val tr2 = TOUR_RESULT.as("tr2")
val inner:Table[Record1[java.lang.Integer]] = DSL.select(count().as("count")).from(tr2).where(tr1.PLAYER_ID.eq(tr2.PLAYER_ID).and(tr1.NSP_SCORE.le(tr2.NSP_SCORE))).asTable("tr3") 
val result = sql.select(tr1.PLAYER_ID,tr1.NSP_SCORE.sum().as("score"))
    .from(tr1)
    .where(inline(3).gt(sql.selectQuery(inner)))
    .groupBy(tr1.PLAYER_ID)
    .orderBy(2)
    .execute()

我目前的问题(使用MariaDB 10和jOOQ 3.9)是为了让这个生成的查询完全适用于MySQL,并按照' Score'排序。 (第2栏)下降。 我确信我可以使用原始SQL语句重写整个语句并转换结果(因此绕过了jOOQ API)。但是,我希望尽可能长时间地使用编译器和jOOQ API作为我的牧羊人。因此,如果我不得不再次触摸此查询,可能会有一个解决方案将来仍然可读。

可以在下面找到说明案例的模式和生成的SQL http://sqlfiddle.com/#!9/2f614f/3在评论中包含违规行/陈述。

    create table TOUR_RESULT (
      player_id int,
      nsp_score int
    );

    insert into TOUR_RESULT values (1,4);
    insert into TOUR_RESULT values (1,14);
    insert into TOUR_RESULT values (1,24);
    insert into TOUR_RESULT values (1,34);
    insert into TOUR_RESULT values (1,44);
    insert into TOUR_RESULT values (2,3);
    insert into TOUR_RESULT values (2,13);
    insert into TOUR_RESULT values (2,23);
    insert into TOUR_RESULT values (2,33);
    insert into TOUR_RESULT values (2,43);
    insert into TOUR_RESULT values (3,3);
    insert into TOUR_RESULT values (3,13);
    insert into TOUR_RESULT values (4,130);
    insert into TOUR_RESULT values (5,2);
    insert into TOUR_RESULT values (5,7);
    insert into TOUR_RESULT values (5,7);
    insert into TOUR_RESULT values (5,7);
    insert into TOUR_RESULT values (5,5);
    insert into TOUR_RESULT values (5,7);
    insert into TOUR_RESULT values (5,10);
    insert into TOUR_RESULT values (5,12);

    SELECT `tr1`.`player_id`, sum(`tr1`.`nsp_score`) AS `score`
      FROM `tour_result` AS `tr1`
      WHERE 3 >=
    --    (SELECT `tr3`.`count`
    --     FROM
     (SELECT count(*) AS `count`
       FROM `tour_result` AS `tr2`
       WHERE (`tr1`.`player_id` = `tr2`.`player_id`
           AND `tr1`.`nsp_score` <= `tr2`.`nsp_score`)) 
    --               AS `tr3`)
    GROUP BY `tr1`.`player_id`
    ORDER BY 2 desc;

取消注释行时的错误是

    Unknown column 'tr1.player_id' in 'where clause'

1 个答案:

答案 0 :(得分:3)

不幸的是,MariaDB和MySQL不允许在相关子查询中引用“两级以上”列。但是如果你有MariaDB 10.2或MySQL 8.0,你可以使用window functions来完成这项工作:

SQL版

SELECT tr1.player_id, SUM(nsp_score) AS score
FROM (
  SELECT 
    tr2.player_id, 
    tr2.nsp_score, 
    ROW_NUMBER () OVER (PARTITION BY tr2.player_id ORDER BY tr2.nsp_score DESC) rn
  FROM tour_result AS tr2
) AS tr1
WHERE rn <= 3
GROUP BY tr1.player_id;

ROW_NUMBER()过滤将在分数中精选3个获胜行。如果您想拥有3行或更多行,如果它们被绑定(WITH TIES语义),则可以使用RANK()I've also blogged about this topic in the past

jOOQ版本

这转换为以下jOOQ查询:

val tr1 = TOUR_RESULT.as("tr1")
val tr2 = TOUR_RESULT.as("tr2")
val result = sql
    .select(tr1.PLAYER_ID, sum(tr1.NSP_SCORE).as("score"))
    .from(table(
       select(
         tr2.PLAYER_ID,
         tr2.NSP_SCORE,
         rowNumber().over(
           partitionBy(tr2.PLAYER_ID)
          .orderBy(tr2.NSP_SCORE.desc())).as("rn"))
      .from(tr2)
    ).as(tr1))
    .where(field(name("rn")).le(inline(3)))
    .groupBy(tr1.PLAYER_ID)
    .fetch()

以上是假设导入

import org.jooq.impl.DSL._