[英]Join two tables based on a foreign key which a substring of column?
I have the two following tables 我有以下两个表
Order 订购
ID| DETAILS | AMOUNT
-------------------------------
0 |#Battery#Client1234 | 90USD
Client 客户
ID | NAME
--------------
1234 | JohnDoe
How can I join these two tables since the foreign key in the Order table is aggregated with some other information ? 由于Order表中的外键已与其他一些信息汇总在一起,因此我如何连接这两个表?
将列DETAILS
分为两个OrderDerails
和CLientID
然后将ClientID与第二个表连接。
http://sqlfiddle.com/#!9/a8c9f6/1 http://sqlfiddle.com/#!9/a8c9f6/1
SELECT o.*, c.*
FROM `order` o
LEFT JOIN client c
ON o.details LIKE CONCAT('%#Client',c.id)
Try: 尝试:
SELECT *
FROM Orders oo
JOIN Client cc
ON oo.details LIKE CONCAT('%', cc.id, '%');
You can extract the client id from the order details using MySQL SUBSTRING_INDEX() function and use it to do a join with the client table. 您可以使用MySQL SUBSTRING_INDEX()函数从订单详细信息中提取客户端ID,并使用它与客户端表进行联接。 Here is the query:
这是查询:
SELECT *
FROM `order` o
LEFT JOIN client c
ON c.id=SUBSTRING_INDEX(o.details,'#Client',-1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.