Hi!
Ich habe diese Abfrage:
var scores = Bookshelf.knex('audition_vote').sum('voting_power as score').groupBy('audition_id')
und ich möchte diese Abfrage wie folgt umschließen:
var topScores = Bookshelf.knex.distinct('score').from(function() {
// This works but i want to use "scores" variable
this.sum('voting_power as score').from('audition_vote').groupBy('audition_id').as('scores');
});
console.log(topScores.toString()) druckt
select distinct `score` from (select sum(`voting_power`) as `score` from `audition_vote` group by `audition_id`) as `scores
Wie verwende ich die Variable "Scores" in from() ????
Was ist die Abfrage, die Sie letztendlich generieren möchten?
Der Letzte:
select distinct `score` from (select sum(`voting_power`) as `score` from `audition_vote` group by `audition_id`) as `scores`
Die Abfrage ist richtig, aber ich möchte die Variable "Scores" wiederverwenden
Oh, Sie können einfach tun:
var scores = Bookshelf.knex('audition_vote')
.sum('voting_power as score')
.groupBy('audition_id')
.as('scores');
var topScores = Bookshelf.knex.distinct('score').from(scores.clone());
Die .as
wird ignoriert, es sei denn, sie befindet sich in einer Unterabfrage, und der Klon ist nur erforderlich, wenn Sie die scores
Abfrage an anderer Stelle mutieren.
Oder Sie könnten tun:
var scores = Bookshelf.knex('audition_vote')
.sum('voting_power as score')
.groupBy('audition_id');
var topScores = Bookshelf.knex.distinct('score').from(scores.clone().as('scores'));
Hilfreichster Kommentar
Oh, Sie können einfach tun:
Die
.as
wird ignoriert, es sei denn, sie befindet sich in einer Unterabfrage, und der Klon ist nur erforderlich, wenn Sie diescores
Abfrage an anderer Stelle mutieren.Oder Sie könnten tun: