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!!
For the exact output you show, you want to join the tables as follows: