【问题标题】:Need some assistance in formatting a MySQL query to return information在格式化 MySQL 查询以返回信息时需要一些帮助
【发布时间】:2012-04-28 05:21:11
【问题描述】:

这个问题是我的另一个问题 @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 超类是必要的(因为玩家/父级和其他“人”实体会有类似的细节),我宁愿留下它。如果你能帮助我得到解决这个问题,我将不胜感激。

提前致谢!!

【问题讨论】:

  • Rob 每个孩子都有两个父母 爸爸和妈妈,你怎么能只带一个父母
  • @ShaikhFarooque 从表格和视觉表现来看,你凭什么说我只带一个父母?
  • Rob会有4列,FatherFirstName,FatherLastName,MotherFirstName,MotherLastName
  • @Rob:你是想为每个孩子获取父母,还是为每个父母获取孩子?
  • @eggyal 在这种情况下,我想为每个父母检索孩子。我想要的结果是直接在“我想要格式化查询,以便它返回如下信息:”下的图形...

标签: mysql sql


【解决方案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)

【讨论】:

  • 哇,太完美了:)。我完全忘记了“ON”操作符——如果我记得这一点,我就不会在这个问题上遇到这么困难了。我不知道你可以像你在这个位中那样拥有motherID和fatherID:“JOIN Player ON (parent.parentID IN (Player.motherID, Player.fatherID))”。这对我来说是一个学习曲线(就像大多数编程语言一样),但我可以在我的数据库中看到其他一些地方,这会派上用场。再次欢呼:)。
【解决方案2】:

请检查一下。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-01
    • 1970-01-01
    • 2019-06-02
    • 1970-01-01
    • 1970-01-01
    • 2011-08-05
    • 1970-01-01
    相关资源
    最近更新 更多