使用另一个表中的值更新一个表中的concat语句?

时间:2016-05-25 16:32:06

标签: mysql

我有一个表,其中包含一个基于其他几个字段的concatenatedTerm字段。根据“名称”字段中的特定值,串联会发生不同的情况,例如

Update Table1 Set ConcatField = concat(Field1,' ',field2) where Name=1;
Update Table1 Set ConcatField = concat(Field1,' ',field2,' ',field3) where Name=2'

为了论证,让我们说Name = 1与FirstLast联合,例如John Smith和名称= 2与Honorific First Last的联合,例如Dr. Joe Blow

我试图通过第二个包含NameConcat字段的表来强制执行此操作。这个想法是我可以制作像

这样的唱片
Table2:Name=1, Table2:Concat=First,' ',Last

然后是单个更新语句,如:

Update Table1 as T1
Inner Join Table2 as T2
On T1.Name=T2.Name
Set T1.ConcatField=T2.Concat

因为Table2已经与其他Name特定数据一起存在,我认为这将允许我简单地向Table2添加Table1 Concats更新的方式,而不必继续更新Table1 Update语句,只需添加Table2,无论如何。

然而,显然问题是update语句正在从T2.Concat中推送实际的字符串,例如所有Name1记录只需获得First Last作为Concat值

=Concact(T2.Concat)

仍然得到同样的错误。所以问题是如何将Table2中的字符串值推送到Table2的更新值中,它实际上是指Table1中的字段?

添加了表格示例 *(note this is not the actual tables or data so I understand in this simple case the solution I'm looking for could be obtained a lot easier, just trying to add simple common table as example)*

表1:

+-----------+-------+--------+------------+
| Honorific | First | Last   | SearchTerm |
+-----------+-------+--------+------------+
| Mr.       | Joe   | Smith  |            |
+-----------+-------+--------+------------+
| Mrs.      | Jane  | Doe    |            |
+-----------+-------+--------+------------+
| Dr.       | Bob   | Rogers |            |
+-----------+-------+--------+------------+

期望输出:

+-----------+-------+--------+-----------------+
| Honorific | First | Last   | SearchTerm      |
+-----------+-------+--------+-----------------+
| Mr.       | Joe   | Smith  | Joe Smith       |
+-----------+-------+--------+-----------------+
| Mrs.      | Jane  | Doe    | Jane Doe        |
+-----------+-------+--------+-----------------+
| Dr.       | Bob   | Rogers | Dr. Bob Rodgers |
+-----------+-------+--------+-----------------+

表2:

+-----------+----------+----------------------+
| Honorific | FullTerm | Concat               |
+-----------+----------+----------------------+
| Mr.       | Mister   | First,Last           |
+-----------+----------+----------------------+
| Mrs.      | Misses   | First,Last           |
+-----------+----------+----------------------+
| Dr.       | Doctor   | Honorific,First,Last |
+-----------+----------+----------------------+

请注意,我真的希望Concat成为第一个,' ',最后但表格生成器不喜欢没有粘贴的引号。

所以这个想法是:

Update Table1 as T1
Inner Join Table2 as T2
On T1.Name=T2.Name
Set T1.SearchTerm=T2.Concat

相当于:

Update Table1 as T1
Inner Join Table2 as T2
On T1.Name=T2.Name
Set T1.SearchTerm=Case
When T2.Name='Dr.' Then Concat(T1.Honorific,T1.First,T1.Last)
Else Concat(T1.First,T1.Last)

并产生与所需输出相同的输出,但它很简单:

+-----------+-------+--------+-----------------------+
| Honorific | First | Last   | SearchTerm            |
+-----------+-------+--------+-----------------------+
| Mr.       | Joe   | Smith  | First,Last            |
+-----------+-------+--------+-----------------------+
| Mrs.      | Jane  | Doe    | First,Last            |
+-----------+-------+--------+-----------------------+
| Dr.       | Bob   | Rogers | Honorific,Search,Last |
+-----------+-------+--------+-----------------------+

换句话说,当我希望它用字段填充Concat语句时,它会将表2中的实际字符串推送到TableT的SearchTerm。

0 个答案:

没有答案