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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:14:17+00:00 2026-06-02T20:14:17+00:00

This question is an extension from another question of mine @ Need some advice

  • 0

This question is an extension from another question of mine @ Need some advice and feedback on coding a many:many relationship in MySQL…

I have the following MySQL code for the tables:

DROP TABLE IF EXISTS `person` ;
CREATE TABLE `person` (
  `personID` INT(5) NOT NULL AUTO_INCREMENT ,
  `firstName` VARCHAR(50) NOT NULL ,
  `lastName` VARCHAR(50) NOT NULL ,
  `dateOfBirth` DATE NOT NULL ,
  `personType` CHAR(6) NOT NULL, 
  `photo` BLOB NULL DEFAULT NULL ,
  PRIMARY KEY (`personID`))
ENGINE = InnoDB;
SHOW WARNINGS;

DROP TABLE IF EXISTS `parent` ;
CREATE TABLE `parent` (
  `parentID` INT(5) NOT NULL,
  PRIMARY KEY (`parentID`), 
  FOREIGN KEY (`parentID`) REFERENCES `person` (`personID`) 
  ON DELETE CASCADE 
  ON UPDATE CASCADE)
ENGINE = InnoDB;
SHOW WARNINGS;

DROP TABLE IF EXISTS `player` ;
CREATE TABLE Player (
    `playerID` INT(5) NOT NULL,
    `motherID` INT(5),
    `fatherID` INT(5),
    `schoolID` INT(5),
    PRIMARY KEY (`playerID`), 
    FOREIGN KEY (`playerID`) REFERENCES `person` (`personID`) 
    ON DELETE CASCADE 
    ON UPDATE CASCADE,
    FOREIGN KEY (`motherID`) REFERENCES `parent` (`parentID`) 
    ON DELETE CASCADE 
    ON UPDATE CASCADE,
    FOREIGN KEY (`fatherID`) REFERENCES `parent` (`parentID`) 
    ON DELETE CASCADE 
    ON UPDATE CASCADE, 
    FOREIGN KEY (`schoolID`) REFERENCES `school` (`schoolID`) 
    ON DELETE CASCADE 
    ON UPDATE CASCADE)
ENGINE = InnoDB;
SHOW WARNINGS;

I am wanting to format a query such that it would return information like so:

+----------+-----------------+----------------+-----------------+----------------+
| ParentID | ParentFirstName | ParentLastName | PlayerFirstName | PlayerLastName |
+----------+-----------------+----------------+-----------------+----------------+
|        1 | John            | Doe            | Maggie          | Doe            |
|        1 | John            | Doe            | Rob             | Doe            |
|        2 | Jane            | Doe            | Rob             | Doe            |
|        2 | Jane            | Doe            | Maggie          | Doe            |
|        3 | Peter           | Smith          | Neil            | Smith          |
|        3 | Peter           | Smith          | Chad            | Smith          |
|        4 | Mary            | Mason          | Neil            | Smith          |
|        4 | Mary            | Mason          | Chad            | Smith          |
+----------+-----------------+----------------+-----------------+----------------+

I notice that names are repeated several times in the above visualization, I’m also wondering if using GROUP_CONCAT would be a good idea to merge them together somehow.

I am having trouble joining these three tables in such a way to generate the query needed. Some inspiration I found (courtesy of a user on here) was found at http://sqlfiddle.com/#!2/baf8d, however I’m having trouble customising this to suit my own needs (in the example the first and last names are in each parent/player table, whereas I inherit these fields from the Person superclass.

The code below was the closest I got before deciding to ask the question on here. I thought somehow if I could merge these queries together to match something like the results above, but I got stuck with it…

mysql> select person.firstName as ParentFirstName, person.lastName as ParentLastName from person where
    -> person.personID IN (select * from parent);
+-----------------+----------------+
| ParentFirstName | ParentLastName |
+-----------------+----------------+
| John            | Doe            |
| Jane            | Doe            |
| Peter           | Smith          |
| Mary            | Mason          |
+-----------------+----------------+

mysql> select person.firstName as ChildFirstName, person.lastName as ChildLastName from person where
    -> person.personID IN (select player.playerID from player);
+----------------+---------------+
| ChildFirstName | ChildLastName |
+----------------+---------------+
| Maggie         | Doe           |
| Rob            | Doe           |
| Neil           | Smith         |
| Chad           | Smith         |
+----------------+---------------+

I felt that having the Person superclass in my database schema was necessary (as the player/parent and other “people” entities would have similar details) and its something I would rather leave in. If you are able to help me get unstuck with this problem I would appreciate it very much.

Thanks in advance!!

  • 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-06-02T20:14:18+00:00Added an answer on June 2, 2026 at 8:14 pm

    For the exact output you show, you want to join the tables as follows:

    SELECT
            parent.parentID  AS ParentID,
      ParentPerson.firstName AS ParentFirstName,
      ParentPerson.lastName  AS  ParentLastName,
       ChildPerson.firstName AS PlayerFirstName,
       ChildPerson.lastName  AS  PlayerLastName
    FROM
           parent
      JOIN Player ON (parent.parentID IN (Player.motherID, Player.fatherID))
      JOIN person AS ParentPerson ON (ParentPerson.personID = parent.parentID)
      JOIN person AS  ChildPerson ON ( ChildPerson.personID = Player.playerID)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some advice. (For those who have already read this before I have
This is an extension to the question I asked here: Get text from clipboard
$(a).each(function() { openFile($(this).attr('href')); } I got this from another question. openFile is a function
This question is an extension to the one raised here: Using factory_girl in Rails
This question already has an answer here: What are Extension Methods? 5 answers Usage
As an extension to this question here Linking JavaScript Libraries in User Controls I
This is an extension for this question asked an hour ago. We cannot modify
This is an extension / next step of this question I asked a few
This is an extension of this question and probably might even be a duplicate
This is an extension of this question: SQLite problem selecting two columns as one

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.