Knex: knex์˜ ๋‹ค๋ฅธ ์ธ์Šคํ„ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” "from" ์ ˆ์˜ ํ•˜์œ„ ์ฟผ๋ฆฌ

์— ๋งŒ๋“  2014๋…„ 10์›” 22์ผ  ยท  3์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: knex/knex

์•ˆ๋…•ํ•˜์„ธ์š”!
์ด ์ฟผ๋ฆฌ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

var scores = Bookshelf.knex('audition_vote').sum('voting_power as score').groupBy('audition_id')

์ด ์ฟผ๋ฆฌ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ž˜ํ•‘ํ•˜๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค.

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()) ์ธ์‡„

select distinct `score` from (select sum(`voting_power`) as `score` from `audition_vote` group by `audition_id`) as `scores

from() ๋‚ด๋ถ€์—์„œ ๋ณ€์ˆ˜ "์ ์ˆ˜"๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• ????

question

๊ฐ€์žฅ ์œ ์šฉํ•œ ๋Œ“๊ธ€

์˜ค, ๋‹น์‹ ์€ ๊ทธ๋ƒฅ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

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

.as ๋Š” ํ•˜์œ„ ์ฟผ๋ฆฌ ๋‚ด์— ์žˆ์ง€ ์•Š์œผ๋ฉด ๋ฌด์‹œ๋˜๋ฉฐ ๋ณต์ œ๋Š” scores ์ฟผ๋ฆฌ๋ฅผ ๋‹ค๋ฅธ ๊ณณ์—์„œ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒฝ์šฐ์—๋งŒ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

๋˜๋Š” ๋‹ค์Œ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

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

๋ชจ๋“  3 ๋Œ“๊ธ€

๊ถ๊ทน์ ์œผ๋กœ ์ƒ์„ฑํ•˜๋ ค๋Š” ์ฟผ๋ฆฌ๋Š” ๋ฌด์—‡์ž…๋‹ˆ๊นŒ?

๋งˆ์ง€๋ง‰ ํ•˜๋‚˜:

select distinct `score` from (select sum(`voting_power`) as `score` from `audition_vote` group by `audition_id`) as `scores`

์ฟผ๋ฆฌ๋Š” ๋งž์ง€๋งŒ "์ ์ˆ˜" ๋ณ€์ˆ˜๋ฅผ ์žฌ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค.

์˜ค, ๋‹น์‹ ์€ ๊ทธ๋ƒฅ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

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

.as ๋Š” ํ•˜์œ„ ์ฟผ๋ฆฌ ๋‚ด์— ์žˆ์ง€ ์•Š์œผ๋ฉด ๋ฌด์‹œ๋˜๋ฉฐ ๋ณต์ œ๋Š” scores ์ฟผ๋ฆฌ๋ฅผ ๋‹ค๋ฅธ ๊ณณ์—์„œ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒฝ์šฐ์—๋งŒ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

๋˜๋Š” ๋‹ค์Œ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

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'));
์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰