์๋
ํ์ธ์!
์ด ์ฟผ๋ฆฌ๊ฐ ์์ต๋๋ค.
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() ๋ด๋ถ์์ ๋ณ์ "์ ์"๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ ????
๊ถ๊ทน์ ์ผ๋ก ์์ฑํ๋ ค๋ ์ฟผ๋ฆฌ๋ ๋ฌด์์ ๋๊น?
๋ง์ง๋ง ํ๋:
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'));
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
์ค, ๋น์ ์ ๊ทธ๋ฅ ํ ์ ์์ต๋๋ค:
.as
๋ ํ์ ์ฟผ๋ฆฌ ๋ด์ ์์ง ์์ผ๋ฉด ๋ฌด์๋๋ฉฐ ๋ณต์ ๋scores
์ฟผ๋ฆฌ๋ฅผ ๋ค๋ฅธ ๊ณณ์์ ๋ณ๊ฒฝํ๋ ๊ฒฝ์ฐ์๋ง ํ์ํฉ๋๋ค.๋๋ ๋ค์์ ์ํํ ์ ์์ต๋๋ค.