茫茫網海中的冷日 - 對這文章發表回應
茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1672039 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
IMG_4447.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

對這文章發表回應

發表限制: 非會員 可以發表

發表者: 冷日 發表時間: 2019/5/27 5:49:42
Update rows in SQL that retain the old value [closed]

My table in SQL
CREATE TABLE Person(
  Person_id INT PRIMARYKEY
  Person_name NVARCHAR(250)
)

Data in table:
INSERT INTO Person VALUES(1,'Peter')

I want update row in SQL that retain the old value.
UPDATE Person SET Person_name = 'Mary' WHERE Person_id = 1

This command does not work. I want after update column Person_name is value 'Peter Mary'



You need to concatenate the literal value Mary with the value of the column Person_name using your RDBMS concatenation operator. But, you didn't specify what RDNMS you are using.

Here is the standard way:
UPDATE Person
SET Person_name = Person_name || ' ' || 'Mary'
WHERE Person_id = 1;

For SQL Server use the + operator like so:
UPDATE Person
SET Person_name = Person_name + ' ' + 'Mary'
WHERE Person_id = 1;




Is it this you want to archieve?

Before the update:
ID | Person_name
---+---------------
1  | Peter

After the update:
ID | Person_name
---+---------------
1  | Peter Mary

If you're for example using MySQL, you could use this approach:
UPDATE Person
SET Person_name = CONCAT(Person_name, ' ', 'Mary')
WHERE Person_id = 1

Just CONCAT a blank and the new value to the current value.
Please don't forget to add the RDBMS you're using. Other RDBMS might have different syntaxes and possibilities.

原文出處:Update rows in SQL that retain the old value - Stack Overflow
內容圖示
url email imgsrc image code quote
樣本
bold italic underline linethrough   












 [詳情...]
validation picture

注意事項:
預覽不需輸入認證碼,僅真正發送文章時才會檢查驗證碼。
認證碼有效期10分鐘,若輸入資料超過10分鐘,請您備份內容後,重新整理本頁並貼回您的內容,再輸入驗證碼送出。

選項

Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|