Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Knex.js count distinct column with alias

Tags:

mysql

knex.js

I want to generate this SQL code:

Select count(distinct user_id) as ui
from posts

I tried this snippet but it did not work:

knex('posts').countDistinct('user_id').as('ui');

How can I do that?

like image 285
Salar Avatar asked Mar 04 '23 18:03

Salar


1 Answers

I found that both count() and countDistinct can parse raw queries like this:

knex('posts').countDistinct('user_id as ui') ;

knex('posts').count('user_id as ui') ;
like image 52
Salar Avatar answered Mar 25 '23 08:03

Salar