Mysql SQL将同一表上的两个不同列连接起来

Mysql SQL将同一表上的两个不同列连接起来,mysql,sql,join,Mysql,Sql,Join,我有一个MySQL数据库表,它指示数据库中某些条目之间的关系。该表有4列: (表名为:RelationsTable) 我正在尝试将此表的数据添加到另一个包含客户ID的表中。但由于客户ID可能同时显示为RelationsTable.ID或RelationsTable.relationId,因此我无法使用普通联接。 我该怎么办 谢谢 你只需要链接它,试试这个 select * from table1, table2 where table1.customer_id = table2.customer

我有一个MySQL数据库表,它指示数据库中某些条目之间的关系。该表有4列: (表名为:RelationsTable)

我正在尝试将此表的数据添加到另一个包含客户ID的表中。但由于客户ID可能同时显示为RelationsTable.ID或RelationsTable.relationId,因此我无法使用普通联接。 我该怎么办


谢谢

你只需要链接它,试试这个

select * from table1, table2 where table1.customer_id = table2.customer_id;
如果仍然存在错误,请更改为

select * from table1, table2 where table1.customer_id == table2.customer_id;

您必须将customer表连接两次,连接到customer表中两个别名不同的列。

我只是在这里猜测,您的帖子中没有多少信息

您可能需要:

SELECT RT.id, Cust.Lastname, Cust.Firstname
FROM RelationsTable RT INNER JOIN Customers Cust
ON RT.id = Cust.id
WHERE RT.type = 'customer'
UNION ALL
SELECT RT.relationid, Cust.Lastname, Cust.Firstname
FROM RelationsTable RT INNER JOIN Customers Cust
ON RT.relationid = Cust.id
WHERE RT.relationtype = 'customer'

请举例说明要将哪些数据插入到另一个表中?请说明您尝试过的代码以及出错的原因。如果客户的ID同时显示为as ID和relationid,那么哪一个优先?或者要同时显示两者?在新表上连接RelationsTable。customerId=RelationsTable.id在新表上连接RelationsTable。customerId=RelationsTable.RelationDuple等号在MySQL中无效:
SELECT RT.id, Cust.Lastname, Cust.Firstname
FROM RelationsTable RT INNER JOIN Customers Cust
ON RT.id = Cust.id
WHERE RT.type = 'customer'
UNION ALL
SELECT RT.relationid, Cust.Lastname, Cust.Firstname
FROM RelationsTable RT INNER JOIN Customers Cust
ON RT.relationid = Cust.id
WHERE RT.relationtype = 'customer'