2013-01-10 25 views
2

我的表列 -如何小写整个字符串保持第一的大写的MYSQL

enter image description here

我的预期产出列改变 -

Smith, Allen, Doyle, Dennis, Baker, Waker 

这是我试过了,但不工作:( -

UPDATE TABLE `employee` 
SET last_name = UCASE(LEFT(lower(last_name), 1)) 

UPDATE TABLE `employee` 
SET last_name = ucase(lower(last_name),1) 

Followed this link too - Resource

错误 -

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'TABLE `employee` SET last_name = UCASE(LEFT(lower(last_name), 1))' at line 1 

让我知道我做错了,以及如何解决。

+0

@sfah可以请你指定的问题更多的哪个部分/答案在我的上下文中是相关的,因为我完全与你为此提供的链接混淆?或者更好地添加一个答案:) – swapnesh

+0

@swapnesh你的表的名字是什么?请参阅下面的答案。 –

+0

@JW。 Thx其工作PLZ根据回答 – swapnesh

回答

7

TABLE是保留关键字。它应该反斜杠逃脱。

我觉得TABLE不应该在您的查询,(我认为这是一个错字

UPDATE employee 
SET last_Name = CONCAT(UCASE(LEFT(last_Name, 1)), LCASE(SUBSTRING(last_Name, 2))) 
+0

感谢您提供的工作解决方案和小提琴,但是您是否可以扩展一些关于反引号的评论,因为我真的觉得不习惯这些时候使用或不使用(尽管在这种情况下它的反向包含和排除也是如此) – swapnesh

+0

@swapnesh这里是如果[MySQL rserved关键字列表](http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html)列表。如果您使用以下任何一种方法,则需要反引号来转义。 –

+0

非常诚挚的谢谢你的链接:) – swapnesh

2

试试这个它可能工作

update `employee` 
set name=concat(left(upper(last_name),1),right(lower(last_name),length(last_name)-1)); 
+0

thx为解决方案:) +1 – swapnesh

0

如果有人这里也在寻找如何发言权同时包含名称列大写名和姓,这里是一个片段,也许有用:

SELECT 
    CONCAT( 
    UCASE(LEFT(substring_index(name, ' ',1),1)), 
    LCASE(SUBSTRING(substring_index(name, ' ',1),2)), 
    ' ', 
    UCASE(LEFT(substring_index(name, ' ',-1),1)), 
    LCASE(SUBSTRING(substring_index(name, ' ',-1),2)) 
    ) as name 
FROM table 
相关问题