我有一个带有firstname,middlename,lastname的表。我想将这3列数据作为全名添加到一个中,就像中间名一样!=''那么fullname = firstname middlename和if lastname!=''然后fullname = firstname middlename lastname。 我尝试如下,但没有行受到影响。
UPDATE studentprofiles SET fullname = CASE middlename WHEN middlename != '' THEN firstname + ' ' + middlename ELSE firstname END WHERE fullname = ''
怎么做?我必须更新多行。欢迎任何帮助/建议。
答案 0 :(得分:1)
您是否尝试使用AND
这样的内容:
CASE
WHEN middlename != '' AND lastname != '' THEN firstname + middlename + lastname
ELSE ''
END AS fullname
答案 1 :(得分:1)
问题解决如下:
UPDATE studentprofiles
SET fullname = CASE
WHEN middlename != '' AND lastname != '' THEN concat(firstname,' ',middlename,' ',lastname)
WHEN middlename != '' AND lastname = '' THEN concat(firstname,' ',middlename)
WHEN middlename = '' AND lastname != '' THEN concat(firstname,' ',lastname)
ELSE concat(firstname)
END
答案 2 :(得分:0)
为什么不能简单地连接列数据,如
UPDATE studentprofiles
SET fullname = concat(firstname, middlename, lastname);
编辑:在这种情况下,您可以使用IFNULL()
功能
UPDATE studentprofiles
SET fullname = IFNULL(firstname,'') +
IFNULL(middlename, '') +
IFNULL(lastname, '');
答案 3 :(得分:0)
尝试使用它:
CASE case_value
WHEN when_value THEN statement_list
[WHEN when_value THEN statement_list] ...
[ELSE statement_list]
END CASE
查看此链接以获取更多信息 enter link description here