簡體   English   中英

在sql中選擇字符串的一部分並使用它來更新列

[英]selecting a portion of string in sql and using it to update a column

我有三張桌子

products->(id, number, public, custom, etc)
references->(id,ref,product_id,price_id)
product_descriptions->(id,product_id,description,summary)

我需要使用表product_descriptions中一部分字符串描述填充表引用中int列的值,這可能嗎?

如何從表的一列中選擇字符串的特定部分並轉換為int並存儲在其他表的列中?

我正在使用MySQL

對於Transact-SQL(SQL Server,SYBASE ASE,ASA,IQ),您可以嘗試此操作。

假設product_description的前4個字符(從位置0開始,長度為4個字符)是要存儲在列reference.ref中的INTEGER值。 在T-SQL中嘗試這樣的事情

UPDATE references SET ref = CAST(SUBSTRING(t2.description,0,4) AS INTEGER)
FROM references AS t1 
INNER JOIN product_descriptions AS t2 
ON t1.product_id = t2.product_id

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM