Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何设计一个;“视图”;在MySQL中,会得到我想要的结果吗?_Mysql - Fatal编程技术网

如何设计一个;“视图”;在MySQL中,会得到我想要的结果吗?

如何设计一个;“视图”;在MySQL中,会得到我想要的结果吗?,mysql,Mysql,我已经设法从MySQL数据库中的一些数据创建了一个视图,如下所示: mysql> CREATE VIEW phonelist AS -> SELECT parent.parentID AS ParentID, -> ParentPerson.firstName AS ParentFirstName, -> ParentPerson.lastName AS ParentLastName, -> ChildPerson.firstNam

我已经设法从MySQL数据库中的一些数据创建了一个视图,如下所示:

mysql> CREATE VIEW phonelist AS
    -> SELECT parent.parentID AS ParentID,
    -> ParentPerson.firstName AS ParentFirstName,
    -> ParentPerson.lastName AS  ParentLastName,
    -> ChildPerson.firstName AS PlayerFirstName,
    -> ChildPerson.lastName AS  PlayerLastName,
    -> GuardianHomePhone.homeNumber AS GuardianHomePhone
    -> 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)
    -> JOIN addressDetails AS GuardianHomePhone ON (GuardianHomePhone.personID = parent.parentID);

Query OK, 0 rows affected (0.00 sec)

mysql> select * from phonelist;

+----------+-----------------+----------------+-----------------+----------------+-------------------+
| ParentID | ParentFirstName | ParentLastName | PlayerFirstName | PlayerLastName | GuardianHomePhone |
+----------+-----------------+----------------+-----------------+----------------+-------------------+
|        8 | Gregory         | Peck           | Michael         | Peck           | 034871234         |
|        9 | Laura           | Peck           | Michael         | Peck           | 034871234         |
|       10 | Martha          | Petersen       | Matt            | Petersen       | 034724321         |
|       10 | Martha          | Petersen       | Christopher     | Petersen       | 034724321         |
|       11 | Chris           | Michaels       | Richard         | Michaels       | 034791212         |
|       11 | Chris           | Michaels       | Shaun           | Michaels       | 034791212         |
|       12 | Nadine          | Michaels       | Richard         | Michaels       | 034791212         |
|       12 | Nadine          | Michaels       | Shaun           | Michaels       | 034791212         |
|       13 | Barry           | Dackers        | Harry           | Dackers        | 034871996         |
|       14 | Kevin           | Mitchell       | Daniel          | Mitchell       | 034742886         |
|       15 | Rebecca         | Mitchell       | Daniel          | Mitchell       | 034742886         |
+----------+-----------------+----------------+-----------------+----------------+-------------------+
11 rows in set (0.00 sec)

mysql>
创建此视图是一项挑战,但下面的描述是我在导航时遇到的问题:

我还需要添加每个球员所在的团队。因为要使团队名称匹配需要将4个表连接在一起,所以我很难将它们合并在一起。我设法创建了一个单独的查询,将玩家与以下团队进行匹配:

mysql> select person.firstName, person.lastName, team.teamName
    -> from person join player on person.personID = player.playerID
    -> join teamAllocation on person.personID = teamAllocation.playerID
    -> join team on teamAllocation.teamID = team.teamID;

+-------------+----------+------------+
| firstName   | lastName | teamName   |
+-------------+----------+------------+
| Michael     | Peck     | U10 Red    |
| Christopher | Petersen | U10 Red    |
| Richard     | Michaels | U11 Orange |
| Shaun       | Michaels | U9 Yellow  |
| Matt        | Petersen | U11 Orange |
| Harry       | Dackers  | U9 Yellow  |
| Daniel      | Mitchell | U9 Yellow  |
+-------------+----------+------------+
7 rows in set (0.00 sec)

mysql>
我脑海中想象的结果是这样的:

+----------+-----------------+----------------+-----------------+----------------+----------------+-------------------+
| ParentID | ParentFirstName | ParentLastName | PlayerFirstName | TeamName       | PlayerLastName | GuardianHomePhone |
+----------+-----------------+----------------+-----------------+----------------+----------------+-------------------+
|        8 | Gregory         | Peck           | Michael         | U10 Red        | Peck           | 034871234         |
|        9 | Laura           | Peck           | Michael         | U10 Red        | Peck           | 034871234         |
|       10 | Martha          | Petersen       | Matt            | U11 Orange     | Petersen       | 034724321         |
|       10 | Martha          | Petersen       | Christopher     | U10 Red        | Petersen       | 034724321         |
|       11 | Chris           | Michaels       | Richard         | U11 Orange     | Michaels       | 034791212         |
|       11 | Chris           | Michaels       | Shaun           | U9 Yellow      | Michaels       | 034791212         |
|       12 | Nadine          | Michaels       | Richard         | U11 Orange     | Michaels       | 034791212         |
|       12 | Nadine          | Michaels       | Shaun           | U9 Yellow      | Michaels       | 034791212         |
|       13 | Barry           | Dackers        | Harry           | U9 Yellow      | Dackers        | 034871996         |
|       14 | Kevin           | Mitchell       | Daniel          | U9 Yellow      | Mitchell       | 034742886         |
|       15 | Rebecca         | Mitchell       | Daniel          | U9 Yellow      | Mitchell       | 034742886         |
+----------+-----------------+----------------+-----------------+----------------+----------------+-------------------+
如果有人能在这方面帮助我,我将非常感激。对于那些想用数据测试查询的人,我已经将模式(DDL)和数据(DML)脚本(放在一个“粘贴”中)放在了


另外,如果有人认为有更好的方法可以查看,请告诉我。

您就快到了,只需将这两个查询组合起来即可

CREATE VIEW phonelistWithTeams AS
SELECT parent.parentID AS ParentID,
  ParentPerson.firstName AS ParentFirstName,
  ParentPerson.lastName AS  ParentLastName,
  ChildPerson.firstName AS PlayerFirstName,
  ChildPerson.lastName AS  PlayerLastName,
  team.teamName AS TeamName,
  GuardianHomePhone.homeNumber AS GuardianHomePhone
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)
  JOIN addressDetails AS GuardianHomePhone ON (GuardianHomePhone.personID = parent.parentID)
  JOIN teamAllocation ON (ChildPerson.personID = teamAllocation.playerID)
  JOIN team ON (teamAllocation.teamID = team.teamID);
运行
SELECT*FROM phone list with teams
将为您提供

+----------+-----------------+----------------+-----------------+----------------+------------+-------------------+
| ParentID | ParentFirstName | ParentLastName | PlayerFirstName | PlayerLastName | TeamName   | GuardianHomePhone |
+----------+-----------------+----------------+-----------------+----------------+------------+-------------------+
|        8 | Gregory         | Peck           | Michael         | Peck           | U10 Red    | 034871234         |
|        9 | Laura           | Peck           | Michael         | Peck           | U10 Red    | 034871234         |
|       10 | Martha          | Petersen       | Matt            | Petersen       | U11 Orange | 034724321         |
|       10 | Martha          | Petersen       | Christopher     | Petersen       | U10 Red    | 034724321         |
|       11 | Chris           | Michaels       | Richard         | Michaels       | U11 Orange | 034791212         |
|       11 | Chris           | Michaels       | Shaun           | Michaels       | U9 Yellow  | 034791212         |
|       12 | Nadine          | Michaels       | Richard         | Michaels       | U11 Orange | 034791212         |
|       12 | Nadine          | Michaels       | Shaun           | Michaels       | U9 Yellow  | 034791212         |
|       13 | Barry           | Dackers        | Harry           | Dackers        | U9 Yellow  | 034871996         |
|       14 | Kevin           | Mitchell       | Daniel          | Mitchell       | U9 Yellow  | 034742886         |
|       15 | Rebecca         | Mitchell       | Daniel          | Mitchell       | U9 Yellow  | 034742886         |
+----------+-----------------+----------------+-----------------+----------------+------------+-------------------+

+1为精确,提供DDL/DML,并使其易于帮助您!我疲惫的眼睛让我忘记了我在AddressDetails表中有一个personID——不应该忘记,因为这是表的主键。你的反馈效果很好,只希望我能以某种方式将重复的值组合在一起(例如,“Martha Petersen”重复了两次,因为她有两个“孩子/玩家”.我尝试使用GROUP_CONCAT,但不确定如何在我的情况下实现它-理想情况下,我希望名字和姓氏的组合不会重复,除非不可避免。除此之外,你的答案很完美,很高兴你花时间帮我:)如果你不想让Martha Petersen出现两次,那意味着马特或克里斯托弗都不会出现。这就是你想要的吗?