[英]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
进入dbshell 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.