0
  eid   |        ename          |    fname       midname  |   lname
--------+-----------------------+-------------+-----------+---------- 
   1            hansen
   2          hansen ola
   3       dennis richard hog

现在如何将它们更新为这样:

  eid   |        ename          |    fname       midname  |   lname
--------+-----------------------+-------------+-----------+---------- 
   1             hansen
   2           hansen ola            hansen        ola
   3       dennis richsard hog       dennis      richard       hog
4

3 回答 3

0

只需从表中获取并使用orename将其分解为名字、中间名和姓氏,并相应地更新表。explode(' ', $name)str_word_count ($name, 1)

用于选择行的 SQL:

SELECT * FROM <tablename>

用于更新行的 SQL:

UPDATE <tablename> SET <columnname> = <value> WHERE ename = <ename>
于 2012-07-04T12:49:59.587 回答
0

您好 检查下面的 SQL 代码以获取更新语句。

CREATE TABLE emptble(eid int,ename varchar(200) NULL,fname varchar(50) NULL,midname varchar(25) NULL,lname varchar(100) NULL)

INSERT INTO emptble(eid,ename)
VALUES(1,'hansen'),(2,'hansen ola'),(3,'dennis richard hog')

SELECT ename,
       CASE WHEN CHARINDEX(' ',ename,1)>0 then LEFT(ename,CHARINDEX(' ',ename,1)-1) ELSE ename END,
       CASE WHEN CHARINDEX(' ',(CASE WHEN CHARINDEX(' ',ename,1) = 0 then NULL ELSE RIGHT(ename,len(ename)-CHARINDEX(' ',ename,1)) END),1)>0 THEN LEFT((CASE WHEN CHARINDEX(' ',ename,1) = 0 then NULL ELSE RIGHT(ename,len(ename)-CHARINDEX(' ',ename,1)) END),CHARINDEX(' ',(CASE WHEN CHARINDEX(' ',ename,1) = 0 then NULL ELSE RIGHT(ename,len(ename)-CHARINDEX(' ',ename,1)) END),1)-1) ELSE (CASE WHEN CHARINDEX(' ',ename,1) = 0 then NULL ELSE RIGHT(ename,len(ename)-CHARINDEX(' ',ename,1)) END) END,
       CASE WHEN CHARINDEX(' ',(CASE WHEN CHARINDEX(' ',ename,1) = 0 then NULL ELSE RIGHT(ename,len(ename)-CHARINDEX(' ',ename,1)) END),1) = 0 THEN NULL ELSE RIGHT(CASE WHEN CHARINDEX(' ',ename,1) = 0 then NULL ELSE RIGHT(ename,len(ename)-CHARINDEX(' ',ename,1)) END,LEN(CASE WHEN CHARINDEX(' ',ename,1) = 0 then NULL ELSE RIGHT(ename,len(ename)-CHARINDEX(' ',ename,1)) END)-CHARINDEX(' ',(CASE WHEN CHARINDEX(' ',ename,1) = 0 then NULL ELSE RIGHT(ename,len(ename)-CHARINDEX(' ',ename,1)) END),1)) END


FROM emptble

UPDATE emptble SET fname= CASE WHEN CHARINDEX(' ',ename,1)>0 then LEFT(ename,CHARINDEX(' ',ename,1)-1) ELSE ename END,
                   midname =CASE WHEN CHARINDEX(' ',(CASE WHEN CHARINDEX(' ',ename,1) = 0 then NULL ELSE RIGHT(ename,len(ename)-CHARINDEX(' ',ename,1)) END),1)>0 THEN LEFT((CASE WHEN CHARINDEX(' ',ename,1) = 0 then NULL ELSE RIGHT(ename,len(ename)-CHARINDEX(' ',ename,1)) END),CHARINDEX(' ',(CASE WHEN CHARINDEX(' ',ename,1) = 0 then NULL ELSE RIGHT(ename,len(ename)-CHARINDEX(' ',ename,1)) END),1)-1) ELSE (CASE WHEN CHARINDEX(' ',ename,1) = 0 then NULL ELSE RIGHT(ename,len(ename)-CHARINDEX(' ',ename,1)) END) END,
                   lname=CASE WHEN CHARINDEX(' ',(CASE WHEN CHARINDEX(' ',ename,1) = 0 then NULL ELSE RIGHT(ename,len(ename)-CHARINDEX(' ',ename,1)) END),1) = 0 THEN NULL ELSE RIGHT(CASE WHEN CHARINDEX(' ',ename,1) = 0 then NULL ELSE RIGHT(ename,len(ename)-CHARINDEX(' ',ename,1)) END,LEN(CASE WHEN CHARINDEX(' ',ename,1) = 0 then NULL ELSE RIGHT(ename,len(ename)-CHARINDEX(' ',ename,1)) END)-CHARINDEX(' ',(CASE WHEN CHARINDEX(' ',ename,1) = 0 then NULL ELSE RIGHT(ename,len(ename)-CHARINDEX(' ',ename,1)) END),1)) END



SELECT * FROM emptble
于 2012-07-06T06:49:20.037 回答
0

str_word_count如果名称始终按相同的顺序排列,您可以简单地使用:检索数组中字符串的单词str_word_count ($ result, 1)

它对你有帮助还是你希望我发展?

于 2012-07-04T12:50:53.957 回答