如何从同一个mySQL表中连接多个数据?

时间:2017-03-03 16:26:04

标签: php mysql left-join

这是我的表position

+----+---------+------+--------+---------+
| id | teacher | cook | doctor | dentist |
+----+---------+------+--------+---------+
|  1 |       3 |    4 |      2 |       1 |
+----+---------+------+--------+---------+

这是我的表people

+----+-----------+--------+-----+
| id | firstname |  name  | age |
+----+-----------+--------+-----+
|  1 | Fred      | Miller |  42 |
|  2 | Emily     | Rose   |  32 |
|  3 | Ben       | Harper |  38 |
|  4 | Samanta   | Jones  |  35 |
+----+-----------+--------+-----+

我的mySQL数据库请求

$pdo = $db->query('
SELECT *, position.id AS id, people.id AS people_id 
FROM position 
LEFT JOIN people 
ON position.teacher=people.id;
ON position.cook=people.id;
ON position.doctor=people.id;
ON position.dentist=people.id;
');

while ($row = $pdo->fetch(PDO::FETCH_ASSOC)) {
echo "The teacher is "$row['firstname']." ".$row['name'];
echo "The cook is "$row['firstname']." ".$row['name'];
echo "The doctor is "$row['firstname']." ".$row['name'];
echo "The dentist is "$row['firstname']." ".$row['name'];
}

我的结果是:

The teacher is Ben Harper
The cook is Ben Harper
The doctor is Ben Harper
The dentist is Ben Harper

我需要的结果:

The teacher is Ben Harper
The cook is Samanta Jones
The doctor is Emiliy Rose
The dentist is Fred Miller

3 个答案:

答案 0 :(得分:2)

如果你重组你的桌子会更好。

+----+------------+
| id | position   |       
+----+------------|
|  1 |  teacher   |
|  2 |  cook      |
|  3 |  doctor    |
|  4 |  dentist   |
+----+------------+


+----+-----------+--------+-----+-------------+
| id | firstname |  name  | age | position_id |
+----+-----------+--------+-----+-------------+
|  1 | Fred      | Miller |  42 |  4          |
|  2 | Emily     | Rose   |  32 |  3          |
|  3 | Ben       | Harper |  38 |  1          |
|  4 | Samanta   | Jones  |  35 |  2          |
+----+-----------+--------+-----+-------------+

这里你可以在people.position_id和position.id上有一个外键引用。 这样你就可以让很多人拥有相同的位置。

SELECT position.id id
     , people.id people_id
     , people.firstname
     , people.name
  FROM position 
  LEFT 
  JOIN people 
    ON people.position_id = position.id;

答案 1 :(得分:1)

如上面的评论和答案中所述,如果您可以重组您的表格,您绝对应该 - 正如其他人所提到的那样,最好重新构建它以获得额外的列,然后您的查询将更简单,更有效。但是如果你被困在一个你无法做到的位置(如果你只提供了大量的数据,那么效率低下并不是太大的问题)那么你可以使用一个UNION将几个简单的查询粘合在一起:

var newData = data.map(function(item) { return item/scalar }

同样,这不是一种有效的方法 - 如果可以,您应该重新构建数据。但是如果你出于某种原因无法做到这一点,那么这将使你在一组行中找回所需的数据,从而拉回所需的数据。

答案 2 :(得分:0)

试试这个,

$pdo = $db->query('
SELECT *, position.id AS id, people.id AS people_id,
(case when position.teacher <> '' then 'teacher'
when position.cook <> '' then 'cook'
when position.doctor <> '' then 'doctor' 
when position.dentist <> '' then 'dentist') position_name
FROM position 
LEFT JOIN people 
ON position.teacher=people.id;
ON position.cook=people.id;
ON position.doctor=people.id;
ON position.dentist=people.id;
');

while ($row = $pdo->fetch(PDO::FETCH_ASSOC)) {
echo "The ".$row['position_name']." is "$row['firstname']." ".$row['name'];
}