Knex: Unterabfrage in der "from"-Klausel mit einer anderen Instanz von knex

Erstellt am 22. Okt. 2014  ·  3Kommentare  ·  Quelle: knex/knex

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() ????

question

Hilfreichster Kommentar

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'));

Alle 3 Kommentare

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'));
War diese Seite hilfreich?
0 / 5 - 0 Bewertungen

Verwandte Themen

rarkins picture rarkins  ·  3Kommentare

koskimas picture koskimas  ·  3Kommentare

marianomerlo picture marianomerlo  ·  3Kommentare

tjwebb picture tjwebb  ·  3Kommentare

aj0strow picture aj0strow  ·  3Kommentare