简体   繁体   English

SQL命令更新表中的列值

[英]SQL command to update column value in table

Following is code on python manage shell 以下是python manage shell代码

>>> User.objects.filter(email__icontains="gmail.com").values_list("email", flat=True)
[u'abc@gmail.com', u'vivekbsable@gmail.com', u'vivek@gmail.com', u'xyz@gmail.com', u'vivekbsable@gmail.com']
>>> for ii in User.objects.filter(email__icontains="gmail.com"):
...      ii.email = ii.email.replace("@gmail.com", "@custom.com")
...      ii.save() 
...     
... 
>>> User.objects.filter(email__icontains="gmail.com").values_list("email", flat=True)
[]
>>> User.objects.filter(email__icontains="@custom.com").values_list("email", flat=True)
[u'vivek@custom.com', u'xyz@custom.com', u'abc@custom.com', u'vivekbsable@custom.com', u'vivekbsable@custom.com']
>>>

I want to write SQL command in Postgresql terminal ( python manage dbshell ) 我想在Postgresql终端中编写SQL命令( python manage dbshell

How can I convert above in SQL command ? 如何在SQL命令中进行上述转换?

Following are my try: 以下是我的尝试:

[Edited1] : [编辑1]

Get Target email ids by SQL command: 通过SQL命令获取目标电子邮件ID:

dp=# SELECT  email FROM auth_user where email LIKE '%@gmail.com';
           email           
---------------------------
 vivek@gmail.com
 xyz@gmail.com
 abc@gmail.com
 vivekbsable@gmail.com
 vivekbsable@gmail.com
(5 rows)

dp=# 

How can I convert above in SQL command? 如何在SQL命令中进行上述转换?

You can have a look at the query Django generates for this, it might not be runnable as in (eg missing params that are sent by Django), but it'll give you a good idea of how Django translates it to SQL 您可以查看Django为此生成的查询,它可能无法像其中那样运行(例如Django发送的缺少参数),但是它将使您对Django如何将其转换为SQL有了一个很好的了解

The idea is to print this value: Model.objects.filter(...).values_list(...).query 想法是打印此值: Model.objects.filter(...).values_list(...).query

query = User.objects.filter(email__icontains="@custom.com").values_list("email", flat=True).query

# Make it print it
print query 
print(query) # Python 3 or with "from future import print_function"

So you want to replace domain in emails, here is test select: 因此,您想替换电子邮件中的域,这是测试选择:

select email, replace(email, '@gmail.com', '@custom.com') as new_email 
from auth_user 
where email like '%@gmail.com';

And update will be: 并将更新为:

update auth_user 
set email = replace(email, '@gmail.com', '@custom.com') 
where email like '%@gmail.com';

Following is my solution: 以下是我的解决方案:

UPDATE auth_user Set email = replace(email, '@gmail.com', '@custom.com') where email LIKE '%@gmail.com';

Demo: 演示:

Go into dbshell 1. cd /var/op/project_name python manage dbshell 进入dbshel​​l 1. cd / var / op / project_name python manage dbshell

dp=# SELECT  email FROM auth_user where email LIKE '%@gmail.com';
           email           
---------------------------
 vivek@gmail.com
 xyz@gmail.com
 abc@gmail.com
 vivekbsable@gmail.com
 vivekbsable@gmail.com
(5 rows)

dp=# SELECT  email FROM auth_user where email LIKE '%@custom.com';
 email 
-------
(0 rows)

dp=# UPDATE auth_user Set email = replace(email, '@gmail.com', '@custom.com') where email LIKE '%@gmail.com';
UPDATE 5

dp=# SELECT  email FROM auth_user where email LIKE '%@gmail.com';
 email 
-------
(0 rows)

dp=# SELECT  email FROM auth_user where email LIKE '%@custom.com';
           email            
----------------------------
 vivek@custom.com
 xyz@custom.com
 abc@custom.com
 vivekbsable@custom.com
 vivekbsable@custom.com
(5 rows)

dp=# 

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

相关问题 使用另一个列值更新 sql db 表列 - Update sql db table columns using another column value 在 SQL 服务器中使用 Python 更新表中列的值 - UPDATE values of a column in a table with Python in SQL Server django中如何更新外表中某列的值 - How to update the value of a column in a foreign table in django 更新 python 中的 postgres 表列值? - update a postgres table column value in python? 使用 python 中的 SqlAlchemy 更新 SQL 表中的值? - Update value in SQL table using SqlAlchemy in python? 数据存储等效于SQL执行(“更新表集合列=%d,其中column2 ='%s'”%(value1,value2))? - Datastore which is equivalent of which is equivalent of SQL execute( “update table set colum=%d where column2='%s'” % (value1,value2))? sqlalchemy中同一表中另一列的sql更新列 - sql update column from another column in the same table in sqlalchemy SQL Python 中的更新命令找不到列并且数据库被锁定 - SQL Update Command in Python cannot find column and database gets locked pandas df.to_sql 如果列值退出替换或更新行 - pandas df.to_sql if column value exits replace or update row 使用python脚本更新列值对表没有影响 - update column value using python script has no effect on table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM