我有两张桌子如下。下面提到的两个表中的Id列值都是自动递增的
的组
+----+-----------+----------------+
| Id |
GroupId |
GroupName |
+----+-----------+----------------+
| 1 |
10 |
Grp1@abc.com |
| 2 |
20 |
Grp2@abc.com |
| 3 |
30 |
Grp3@xyz.com |
| 4 |
40 |
Grp4@def.com |
+----+-----------+----------------+
客户
+---+-----------------+------------+----------+---------------+
| Id |
GroupAliasName |
Domain |
GroupId |
CustomerName |
+---+-----------------+------------+----------+---------------+
| 1 |
Grp1 |
abc.com |
null |
Cust1 |
| 2 |
Grp2 |
abc.com |
null |
Cust2 |
| 3 |
Grp3 |
xyz.com |
null |
Cust3 |
| 4 |
Grp4 |
def.com |
null |
Cust4 |
+---+-----------------+------------+----------+---------------+
现在来自客户表格'GroupAliasName'
和'Domain'
,当'GroupAliasName@Domain'
连接时相当于群组中的'GroupName'
表
使用客户表中的连接值,我需要从组表中提取'GroupId'
并在客户中填充> 表格'GroupId'
如下
客户
+----+----------------+------------+----------+---------------+
| Id |
GroupAliasName |
Domain |
GroupId |
CustomerName |
+----+----------------+-----------+---------+-----------------+
| 1 |
Grp1 |
abc.com |
10 |
Cust1 |
| 2 |
Grp2 |
abc.com |
20 |
Cust2 |
| 3 |
Grp3 |
xyz.com |
30 |
Cust3 |
| 4 |
Grp4 |
def.com |
40 |
Cust4 |
+----+----------------+------------+----------+---------------+
我尝试的查询如下
UPDATE Customer SET GroupId =
(SELECT GroupId FROM Group G
WHERE GroupName =
(SELECT CONCAT(GroupAliasName, '@', Domain) AS GroupName
FROM Customer
WHERE Domain IS NOT NULL) AND G.GroupName = GroupName);
但我收到的错误为'Subquery returns more than 1 row'
请建议或提供您的意见。
答案 0 :(得分:1)
尝试这样的事情
UPDATE Customer as c
INNER JOIN Group as g on ( CONCAT(c.GroupAliasName, '@', c.Domain) = g.GroupName)
SET c.GroupId = g.GroupId;
答案 1 :(得分:1)
试试这个:
update customer as cust
inner join `group` grp on concat(cust.groupaliasname, '@', cust.domain) = grp.groupname
set cust.groupId = grp.groupId;