B
B
BonBon Slick2017-10-24 12:42:37
JavaScript
BonBon Slick, 2017-10-24 12:42:37

SELECT DISTINCT, ORDER BY expressions must appear in select?

User::select( DB::raw('DISTINCT(user.id) as user_id'), 'users.role_id')
//                    ->distinct()
                ->join('roles', 'users.role_id', '=', 'roles.id')
//                ->sharedLock() 
                ->inRandomOrder()
                 ->orderByDesc('user_id')
                ->limit($limit)
                ->get();

Tried to add as DB::raw , throws an error. I tried to use the ->distinct() function , it throws an error. Tried with and without orderByDesc("user_id") . Also tried DISTINCT ON(...), error.
ERROR:
Invalid column reference: 7 ERROR:  for SELECT DISTINCT, ORDER BY expressions must appear in select list↵LINE 1: ...10

Syntax error: 7 ERROR:  syntax error at or near ","↵LINE 1: select DISTINCT ON(users.id),

https://www.postgresql.org/docs/9.6/static/sql-sel...
Please tell me how to do it right?
I think the error is here:
...
  ->inRandomOrder()
...

..."users"."deleted_at" is null order by RANDOM() 1...

And the error occurs due to the fact that it is necessary to make a random selection of unique values.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2019-07-25
@OldSchool1705

does not work because there is only a function definition, not a call ... then either call the function, or declare this function as "automatically started"

(function getBiggerBook(book) {
 .....
})(Book)

B
BonBon Slick, 2017-10-24
@BonBonSlick

PostgreSQL does not support random() + distinct() sampling by standard means, see below for information and possible solutions.
https://github.com/laravel/framework/issues/20457
https://www.postgresql.org/message-id/5070.1286027...
https://stackoverflow.com/questions/28371105/selec...
https ://stackoverflow.com/questions/11401229/how-t...
https://stackoverflow.com/questions/8446839/select...
https://stackoverflow.com/questions/24823915/how-t.. https :
//stackoverflow.com/questions/34870957/subqu...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question