这个问题是我的另一个问题@ Need some advice and feedback on coding a many:many relationship in MySQL ...
的延伸我有以下表格的MySQL代码:
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;
我想格式化一个查询,以便返回如下信息:
+----------+-----------------+----------------+-----------------+----------------+
| 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 |
+----------+-----------------+----------------+-----------------+----------------+
我注意到在上面的可视化中,名称会重复多次,我也想知道是否使用GROUP_CONCAT以某种方式合并它们是个好主意。
我无法以这种方式加入这三个表来生成所需的查询。我在http://sqlfiddle.com/#!2/baf8d找到了一些我发现的灵感(由用户提供),但是我无法根据自己的需要进行自定义(在示例中,名字和姓氏都在每个父/玩家表中,而我从Person超类继承了这些字段。
下面的代码是我在决定在这里提问之前最接近的代码。我想不知怎的,如果我可以将这些查询合并在一起以匹配上面的结果,但我一直坚持下去......
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 |
+----------------+---------------+
我觉得在我的数据库模式中拥有Person超类是必要的(因为玩家/父级和其他“人”实体会有类似的细节)和我宁愿留下的东西。如果你能帮助我如果不解决这个问题,我会非常感激。
提前致谢!!
答案 0 :(得分:1)
请检查一下。
select
(select P.FirstName from Parent as P where P.ParentID = pl.FatherID ) as FatherName,
(select P.FirstName from Parent as P where P.ParentID = pl.MotherID ) as MotherName,
pl.FirstName as PlayerFirstName, pl.LastName as PlayerLastName from player pl
答案 1 :(得分:1)
对于您显示的确切输出,您希望按如下方式加入表:
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)