我有一个表,它使用到达时中断的外部数据。我可以从未被切断的表中完成它吗(因为它们使用替代的外部数据)?
在这个例子中,第 2 行和第 3 行可以使用它们各自表中的匹配数据来完成。
Table1:
Table | Type | Description
X | A | A long line th
X | B | A long line th
Y | B | This line d
Y | A | This line d
TableX:
Type | Description
A | A very long line that doesn't get cut off
B | A long line that doesn't get cut off
TableY:
Type | Description
A | just a long line that doesn't get cut off
B | This line doesn't get cut off
Needed result
===============
Table1:
Project | Type | Description
X | A | A long line th
X | B | A long line that doesn't get cut off
Y | B | This line doesn't get cut off
Y | A | This line d
最佳答案
尝试这样的事情......
SELECT `Table` as Project,Type,Description
FROM Table1 T1 WHERE Type = 'A'
UNION ALL
SELECT `Table`,T1.Type,T2.Description
FROM Table1 T1 INNER JOIN TableX T2
ON T1.`Table` = 'X' AND T1.Type = 'B' AND T1.Type = T2.Type
UNION ALL
SELECT `Table`,T1.Type,T2.Description
FROM Table1 T1 INNER JOIN TableY T2
ON T1.`Table` = 'Y' AND T1.Type = 'B' AND T1.Type = T2.Type
http://www.sqlfiddle.com/#!9/b40a3/6
SELECT `Table` as Project,Type,Description
FROM Table1 T1 WHERE Type = 'A'
UNION ALL
SELECT `Table`,T1.Type,T2.Description
FROM Table1 T1 INNER JOIN TableX T2
ON T1.`Table` = 'X' AND T1.Type = 'B' AND T1.Type = T2.Type
AND T2.Description like CONCAT(T1.Description,"%")
UNION ALL
SELECT `Table`,T1.Type,T2.Description
FROM Table1 T1 INNER JOIN TableY T2
ON T1.`Table` = 'Y' AND T1.Type = 'B' AND T1.Type = T2.Type
AND T2.Description like CONCAT(T1.Description,"%")
如果你也想匹配描述,你可以使用像
这样的东西关于mysql - 根据另一个表完成文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37887142/