Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How can I translate this to knex.js?

SELECT id, 
       Sum(x.kills) AS total 
FROM   (SELECT id, 
               kills 
        FROM   bedwars_player_solo 
        UNION ALL 
        SELECT id, 
               kills 
        FROM   bedwars_player_duo 
        UNION ALL 
        SELECT id, 
               kills 
        FROM   bedwars_player_comp_clan) x 
GROUP  BY id 
ORDER  BY total DESC 
LIMIT  10; 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
913 views
Welcome To Ask or Share your Answers For Others

1 Answer

I believe you could write it as

knex.select('id', knex.raw('SUM(x.kills)'))
.from(function() {
  knex.select('id','kills').from('bedwars_player_solo')
  .unionAll(
    knex.select('id','kills').from('bedwars_player_duo'),
    knex.select('id','kills').from('bedwars_player_comp_clan')
).as('x')
}).groupBy('id')
.orderBy('total', 'desc')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...