简体   繁体   English

使用子查询更新列

[英]UPDATE a column with a subquery

I have aa user table (tbUser) with a column emailaddress and username and I'm trying to get rid of any email address in the username column. 我有一个带有电子邮件地址和用户名列的用户表(tbUser),并且我试图摆脱用户名列中的任何电子邮件地址。 The below query finds all records where the person used an '@' in their username and I want to just take everything to the left of the '@' -- this is good enough for me to figure out if it's an email address don't need to be too strict whether it's actually an email address. 下面的查询查找该用户在用户名中使用了“ @”的所有记录,而我想将所有内容都保留在“ @”的左侧-这足以让我弄清楚它是否是电子邮件地址“ don”是否实际上是电子邮件地址,不必太严格。

SELECT LEFT(username, CHARINDEX('@', username) - 1) AS Expr1
FROM     tbUser
WHERE  (username LIKE '%@%')

Now I want to UPDATE all usernames so that it's only the text to the left of the '@' but not sure how to do it. 现在,我想更新所有用户名,以便仅是“ @”左侧的文本,但不确定如何操作。 It's something like the below query but it's not quite right. 这类似于下面的查询,但是不太正确。

UPDATE tbUser
SET username = (SELECT LEFT(username, CHARINDEX('@', username) - 1) AS Expr1
                FROM tbUser AS tbUser_1)
WHERE  (username LIKE '%@%')

If that SELECT returns exactly what you want you should be able to update the table just as: 如果SELECT恰好返回了您想要的内容,则您应该能够像下面这样更新表:

UPDATE tbUser
SET username = LEFT(username, CHARINDEX('@', username) - 1)
WHERE username LIKE '%@%'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM