Basic Query - Order By Count

Posted 4 years ago by itstrueimryan

Not sure what I'm doing wrong, but this isn't producing the right results. The query I want to replicate is simple - no relationships or anything. Just trying to get the tag_ids from tag_topic with the highest number of occurrences:

SELECT tag_id FROM `tag_topic` GROUP BY tag_id ORDER BY COUNT(tag_id) DESC LIMIT 8

And here is my Eloquent code:

$tagIds = \DB::table('tag_topic')
            ->groupBy('tag_id')
            ->orderBy(\DB::raw('count(tag_id)', 'DESC'))
            ->take(8)
            ->lists('tag_id');

And I just get some really wacky results, clearly not the right ones.

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.