我认为对于那些拥有更多SQL经验的人来说,这是一个简单的答案。我有两张桌子。一个表(网站)中的两个字段引用另一个字段中的一个字段(ip_address)。我在创建将打印以下输出的SELECT命令时遇到问题:
MariaDB [test]> select * from website;
+----+-----------+--------------+----------------+
| id | node_name | primary_host | secondary_host |
+----+-----------+--------------+----------------+
| 1 | site1 | 1 | 2 |
| 2 | site2 | 3 | 4 |
+----+-----------+--------------+----------------+
2 rows in set (0.00 sec)
MariaDB [test]> select * from ip_address;
+----+-------------+
| id | ip |
+----+-------------+
| 1 | 192.168.1.1 |
| 2 | 192.168.1.2 |
| 3 | 192.168.1.3 |
| 4 | 192.168.1.4 |
+----+-------------+
4 rows in set (0.00 sec)
需要输出:
+-----------+-------------+-------------+
| node_name | ip | ip |
+-----------+-------------+-------------+
| site1 | 192.168.1.1 | 192.168.1.2 |
| site2 | 192.168.1.3 | 192.168.1.4 |
+-----------+-------------+-------------+
2 rows in set (0.00 sec)
答案 0 :(得分:0)
我认为这可以解决你的问题。
select v1.node_name, v2.ip, v3.ip
from website v1
left join ip_address v2 on v1.primary_host = v2.id
left join ip_address v3 on v1.secondary_host = v3.id