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 6965227
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:01:48+00:00 2026-05-27T16:01:48+00:00

I’m creating an application that allow users to create group profiles. A group profile

  • 0

I’m creating an application that allow users to create “group profiles.” A group profile represents one or more individuals. At the basic level, I’m using 2 tables: a parent table to group the profiles and a child table for the actual profiles. Assume that the profiles had age and gender details, how would I query the tables to get a list of parent table ids of:

  • group profiles that represent just one profile?
  • group profiles that represent a male and female between the ages of 25 and 30?
  • group profiles that represent more than 2 profiles?
  • etc.

At some point, I think I need to do a GROUP BY on the parent table id, but I’m a bit lost how to check the number of profiles a group profile has, how to meet certain criteria in the profiles (e.g. gender, etc.).

Here’s a sample table with some data:

SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL';

DROP SCHEMA IF EXISTS `mydb` ;
CREATE SCHEMA IF NOT EXISTS `mydb` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci ;
USE `mydb` ;

-- -----------------------------------------------------
-- Table `mydb`.`foo_parent`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`foo_parent` ;

CREATE  TABLE IF NOT EXISTS `mydb`.`foo_parent` (
  `foo_parent_id` INT NOT NULL AUTO_INCREMENT ,
  PRIMARY KEY (`foo_parent_id`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `mydb`.`foo`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `mydb`.`foo` ;

CREATE  TABLE IF NOT EXISTS `mydb`.`foo` (
  `foo_id` INT NOT NULL AUTO_INCREMENT ,
  `foo_parent_id` INT NOT NULL ,
  `name` CHAR(50) NOT NULL ,
  `gender` ENUM('male','female') NOT NULL ,
  `age` INT NOT NULL ,
  PRIMARY KEY (`foo_id`) ,
  INDEX `foo_foo_parent_id` (`foo_parent_id` ASC) ,
  CONSTRAINT `foo_foo_parent_id`
    FOREIGN KEY (`foo_parent_id` )
    REFERENCES `mydb`.`foo_parent` (`foo_parent_id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;



SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

-- -----------------------------------------------------
-- Data for table `mydb`.`foo_parent`
-- -----------------------------------------------------
START TRANSACTION;
USE `mydb`;
INSERT INTO `mydb`.`foo_parent` (`foo_parent_id`) VALUES (1);
INSERT INTO `mydb`.`foo_parent` (`foo_parent_id`) VALUES (2);

COMMIT;

-- -----------------------------------------------------
-- Data for table `mydb`.`foo`
-- -----------------------------------------------------
START TRANSACTION;
USE `mydb`;
INSERT INTO `mydb`.`foo` (`foo_id`, `foo_parent_id`, `name`, `gender`, `age`) VALUES (1, 1, 'John Doe', 'male', 31);
INSERT INTO `mydb`.`foo` (`foo_id`, `foo_parent_id`, `name`, `gender`, `age`) VALUES (2, 1, 'Jane Doe', 'female', 29);
INSERT INTO `mydb`.`foo` (`foo_id`, `foo_parent_id`, `name`, `gender`, `age`) VALUES (3, 2, 'Billy Bob', 'male', 25);
INSERT INTO `mydb`.`foo` (`foo_id`, `foo_parent_id`, `name`, `gender`, `age`) VALUES (4, 2, 'Suzie', 'female', 27);

COMMIT;

Sample queries and results:

Example 1: get all foo_parent.id where the profiles represented by the group profile includes a “male” and a “female” returns 1 and 2.

Example 2: get all foo_parent.id where the profiles represented by the group profile includes a “male” and a “female” and are between the ages of 20 and 30 returns 2.

Also: if there is a blatantly obvious problem with this approach of grouping and querying profiles, please let me know.

  • 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-27T16:01:49+00:00Added an answer on May 27, 2026 at 4:01 pm

    You could try building your predicate counts e.g. ‘males between 20 and 30’ or ‘females between 20 and 30’ in an inline view and then referring to them in the where clause of the outer query. For example:

    Example 1: get all foo_parent.id where the profiles represented by the group profile includes a “male” and a “female” returns 1 and 2:

    select t.foo_parent_id
    from
    (
    select fp.foo_parent_id,
    sum(case when f.gender = 'Male' then 1 else 0 end) as males,
    sum(case when f.gender = 'Female' then 1 else 0 end) as females,
    sum(case when f.gender = 'Male' and f.age between 20 and 30 then 1 else 0 end) as twenties_males,
    sum(case when f.gender = 'Female' and f.age between 20 and 30 then 1 else 0 end) as twenties_females
    from foo_parent fp
    left outer join foo f on f.foo_parent_id = fp.foo_parent_id
    group by fp.foo_parent_id
    ) t
    where t.males > 0 and t.females > 0;
    

    Example 2: get all foo_parent.id where the profiles represented by the group profile includes a “male” and a “female” and are between the ages of 20 and 30 returns 2:

    select t.foo_parent_id
    from
    (
    select fp.foo_parent_id,
    sum(case when f.gender = 'Male' then 1 else 0 end) as males,
    sum(case when f.gender = 'Female' then 1 else 0 end) as females,
    sum(case when f.gender = 'Male' and f.age between 20 and 30 then 1 else 0 end) as twenties_males,
    sum(case when f.gender = 'Female' and f.age between 20 and 30 then 1 else 0 end) as twenties_females
    from foo_parent fp
    left outer join foo f on f.foo_parent_id = fp.foo_parent_id
    group by fp.foo_parent_id
    ) t
    where t.twenties_males > 0 and t.twenties_females > 0;
    

    I’ve included the foo_parent table in these queries in case you want to include something like ‘has no males or females between 20 and 40’ or something like that.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I need a function that will clean a strings' special characters. I do NOT
I am using Paperclip to handle profile photo uploads in my app. They upload

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.