[英]Updating SQL Query on multiple values
I have two models one is student_profile where I have university field to show university name. 我有两个模型,一个是student_profile,在那里我可以显示大学名称。 I have a list of universities where I need to update another table called Package only if one of the university exists in table. 我有一所大学列表,仅当表中有其中一所大学时,我才需要更新另一个表(称为“包”)。 Table has 1000
records and I need to update all the entries with one query. 表有1000
条记录,我需要用一个查询更新所有条目。
a, b, c, d
exists in student_profile
. 如果大学a, b, c, d
存在于student_profile
。 My tables: 我的桌子:
+---------------------------+
| student_profile |
+---------------------------+
| id | int(11) |
| first_name | varchar(45) |
| last_name | varchar(45) |
| university | varchar(45) |
+---------------------------+
+---------------------------+
| package |
+---------------------------+
| student_id | int(11) |
| is_active | tinyint |
| package_type| varchar(45) |
+---------------------------+
ForeignKeys
in StudentProfile
Table: ForeignKeys
在StudentProfile
表:
name = student_package
schema = mydb
Column = student_id
reference Schema = mydb
referenced table = student_profile
referenced column= id
If university exists I need to set is_active=True
and set package.student_id as student_profile.id
and package.package_type as
' UniverityEnrolled
'. 如果大学存在,我需要设置is_active=True
并将package.student_id as student_profile.id
设置package.student_id as student_profile.id
并将package.package_type as
' UniverityEnrolled
'。
Based on what I understand of the question, this may be your solution: 根据我对问题的了解,这可能是您的解决方案:
UPDATE package
SET is_active = 1,package_type = "UniversityEnrolled"
WHERE student_id IN
(SELECT id FROM student_profile WHERE university IN ("a","b","c","d"))
To figure something like this out, start with a SELECT that outputs the records to be updated. 为了弄清楚这样的事情,请从SELECT开始,该命令输出要更新的记录。
Then when it is working, convert to an update statement. 然后,在工作时,将其转换为更新语句。
SELECT *
FROM `StudentProfile` a
JOIN `Package` b
ON a.`id` = b.`student_id`
WHERE `university` in ('a','b','c');
UPDATE `StudentProfile` a
SET `is_active` = 1
JOIN `Package` b
ON a.`id` = b.`student_id`
WHERE `university` in ('a','b','c');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.