Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6698631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:36:03+00:00 2026-05-26T06:36:03+00:00

How can a sort by user popularity when using a hasAndBelongsToMany table relationship? ie:

  • 0

How can a sort by user popularity when using a hasAndBelongsToMany table relationship? ie:

I have a table called playgroups, players, and playgroup_players.

How can I sort playgroups by the number of players in the playgroup?

Playgroups:

CREATE TABLE `playgroups` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `tag` varchar(4) COLLATE utf8_unicode_ci DEFAULT NULL,
  `description` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `avatar_url` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `city` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `state` varchar(2) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Players:

CREATE TABLE `players` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `first_name` varchar(30) DEFAULT NULL,
  `last_name` varchar(30) DEFAULT NULL,
  `username` varchar(30) NOT NULL,
  `password` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  `public_email` tinyint(1) NOT NULL DEFAULT '0',
  `facebook_id` varchar(250) DEFAULT NULL,
  `card_tooltip` tinyint(1) NOT NULL DEFAULT '1',
  `profile_pic_url` varchar(255) NOT NULL,
  `public_name` tinyint(1) NOT NULL DEFAULT '0',
  `city` varchar(40) NOT NULL,
  `state` varchar(2) NOT NULL,
  `date_joined` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=107 DEFAULT CHARSET=latin1;

And PlaygroupPlayers:

CREATE TABLE `playgroup_players` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `player_id` int(11) NOT NULL,
  `playgroup_id` int(11) NOT NULL,
  `admin` tinyint(1) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:36:03+00:00Added an answer on May 26, 2026 at 6:36 am
    SELECT PP.playgroup_id, COUNT(*) cnt
    FROM playgroup_players PP
    GROUP BY PP.playgroup_id
    ORDER BY COUNT(*) DESC
    

    This will give you a list of playgroups that have at least one player sorted by the number of players. Of course, field name is made up 🙂

    SELECT G.playgroup_id, COUNT(PP.playgroup_id) cnt
    FROM playgroup G
      LEFT OUTER JOIN playgroup_players PP ON (PP.playgroup_id=G.playgroup_id)
    GROUP BY G.playgroup_id
    ORDER BY COUNT(*) DESC
    

    This should give you a list of ALL playgroups (even the ones with no players). I’ve tested this on Oracle and on some of my own data and it works

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using JSF and I have a complex datatable where the user can sort
I have a table of user-uploaded objects. Each user can have an arbitrary number
I have a list of fields which a user can sort using jquery. I
So, right now I've got a number of columns the user can sort by
I'm using a standard GridView with a LinqDataSource. The user can sort and page
I know I can use something like User.sort {|a, b| a.attribute <=> b.attribute} or
I can't get my GridView to enable a user to sort a column of
I am looking at some sort of existing filter which can sanitize the user
Can SQLite sort naturally? For example, CREATE TABLE animals ( id INTEGER NOT NULL
A user can manually sort files in a standard Windows Open Dialog (in Details

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.