[英]SQL JOIN two different columns on the same table
I have a MySQL Database Table that indicates the relations between some entries in my database. 我有一个MySQL数据库表,用于指示数据库中某些条目之间的关系。 the table has 4 columns: (the table name is: RelationsTable)
该表有4列:(表名称为:RelationsTable)
id | type | relationId | relationType
1 | customer | 5 | recipt
2 | recipt | 4 | customer
I'm trying to add this table's data to another table that contains a customer ID. 我正在尝试将此表的数据添加到另一个包含客户ID的表中。 But since the customer's ID might appear both as RelationsTable.id or RelationsTable.relationId , I can't use a normal JOIN.
但是由于客户的ID可能同时显示为RelationsTable.id或RelationsTable.relationId,因此我不能使用普通的JOIN。 What should I do?
我该怎么办?
thanks! 谢谢!
you just need to link it, try this 您只需要链接它,试试这个
select * from table1, table2 where table1.customer_id = table2.customer_id;
if still error change to 如果仍然错误更改为
select * from table1, table2 where table1.customer_id == table2.customer_id;
您必须两次将客户表连接到两个具有不同客户表别名的列。
I'm just guessing here with not much info from your post. 我只是在这里猜测您的帖子没有太多信息。
You probably need this: 您可能需要此:
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'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.