XF 2.1 GROUP BY and LIMIT raw query

Cupara

Well-known member
I'm sure there is something wrong with this query but I can't figure it out, I keep getting an error to check my syntax to use near GROUP BY tips.user_id

PHP:
$tipsters = $db->query('
            SELECT floor(tips.user_id / 1000) AS image, xf_user.*, tips.user_id AS userId, tips.status AS status, SUM(tips.winnings) AS winnings, COUNT(1) AS tips
                FROM xf_user
                INNER JOIN tips ON (tips.user_id = xf_user.user_id)
                WHERE winnings != 0 AND status != 1
                ORDER BY winnings DESC, tips DESC
                LIMIT '.$page.','.$perPage
                GROUP BY tips.user_id
            );

Here is the error:
Code:
XF\Db\InvalidQueryException: MySQL statement prepare error [1064]: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'GROUP BY tips.user_id' at line 7 in src/XF/Db/AbstractStatement.php at line 217
[LIST=1]
[*]XF\Db\AbstractStatement->getException() in src/XF/Db/Mysqli/Statement.php at line 196
[*]XF\Db\Mysqli\Statement->getException() in src/XF/Db/Mysqli/Statement.php at line 39
[*]XF\Db\Mysqli\Statement->prepare() in src/XF/Db/Mysqli/Statement.php at line 54
[*]XF\Db\Mysqli\Statement->execute() in src/XF/Db/AbstractAdapter.php at line 94
[*]XF\Db\AbstractAdapter->query() in src/addons/BetClever/Tipsters/Pub/Controller/Tipsters.php at line 40
[*]BetClever\Tipsters\Pub\Controller\Tipsters->actionIndex() in src/XF/Mvc/Dispatcher.php at line 321
[*]XF\Mvc\Dispatcher->dispatchClass() in src/XF/Mvc/Dispatcher.php at line 248
[*]XF\Mvc\Dispatcher->dispatchFromMatch() in src/XF/Mvc/Dispatcher.php at line 100
[*]XF\Mvc\Dispatcher->dispatchLoop() in src/XF/Mvc/Dispatcher.php at line 50
[*]XF\Mvc\Dispatcher->run() in src/XF/App.php at line 2177
[*]XF\App->run() in src/XF.php at line 390
[*]XF::runApp() in index.php at line 20
[/LIST]
 
Last edited:
For anyone wondering, I have figured this out.

First fetch your data:
PHP:
$data = $finder->order('winnings', 'DESC')->where('winnings', '!=', '0')->fetch();

Then after you fetch your data:
PHP:
$tipsters = $data->groupBy('user_id');
 
Back
Top Bottom