mysql - 根据另一个表完成文本

标签 mysql string text

我有一个表,它使用到达时中断的外部数据。我可以从未被切断的表中完成它吗(因为它们使用替代的外部数据)?

在这个例子中,第 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,"%")

如果你也想匹配描述,你可以使用像

这样的东西

http://www.sqlfiddle.com/#!9/b40a3/9

关于mysql - 根据另一个表完成文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37887142/

相关文章:

c - 如何从字符串中获取字符串(C 中的字符指针)

mysql - 错误 1054 (42S22) : Unknown column

c++ - 使用 mysql++ 构建应用程序时找不到 mysql_version.h

php - Laravel 4 中的列歧义错误

string - MATLAB。查找字符串元胞数组的索引,其中所有字符都包含在给定字符串中(不重复)

c - 我应该使用 fputs 还是 putchar?

r - 如何使用R创建单词共现矩阵

javascript - HTML5 Text Canvas 在文本宽度大于允许的最大宽度时旋转

r - 从单个字符串中提取多个子字符串

mysql - 我可以将一个 RDS 复制到另一个 RDS,它是它的一个子集吗?