我有2张桌子
+------+-------+-------+-------+-------+
| id | start | end | pointA| pointB|
+------+-------+-------+-------+-------+
| 1 | xxx | xx | 1 | 2 |
| 2 | xxx | xx | 2 | 1 |
+------+-------+-------+-------+-------+
+------+-------------+
| id | desc |
+------+-------------+
| 1 | "Chicago" |
| 2 | "NYC" |
+------+-------------+
例如:
当一个人从A点骑到B点时,它会被记录在rides
表中。 pointA
和pointB
都是表rides
points
表格中的FK
如何以MySQL
的方式获取id = 1
的SQL输出,例如+------+-------+-------+-------+-------+
| id | start | end | pointA| pointB|
+------+-------+-------+-------+-------+
| 1 | xxx | xx |Chicago| NYC |
:
{{1}}
答案 0 :(得分:1)
您可以通过为每个表添加不同的别名来连接同一个表:
SELECT r.id, r.start, r.end, pA.desc as pointA, pB.desc as pointB
FROM rides r
JOIN points pA ON pA.id = r.pointA
JOIN points pB ON pB.id = r.pointB
WHERE r.id = 1;