简体   繁体   English

更新查询INNER JOIN mysqli

[英]Update query INNER JOIN mysqli

If I want to make an update query using INNER JOIN , how do I do in this case: 如果我想使用INNER JOIN进行更新查询,在这种情况下,我该怎么办:

I have a users table & user_settings table. 我有一个users表和user_settings表。 users contains column id and user_settings contains column userid . users包含列iduser_settings包含列userid I want to do like: 我想这样做:

UPDATE users_settings SET commets = 0 WHERE email = "$email";

But user_settings does not contain email , so I want to pick the email from the users table where the id is userid . 但是user_settings不包含email ,因此我想从iduseridusers表中选择email

Try this: 尝试这个:

UPDATE user_settings s 
INNER JOIN users u ON s.userid=u.id
SET commets = 0 WHERE email = "$email";

Note: There should be no users with the same e-mail in these cases. 注意:在这种情况下,不应有用户使用相同的电子邮件。

You can use this query. 您可以使用此查询。 it will work 它会工作

UPDATE users_settings SET commets = 0 WHERE userid
IN (SELECT id FROM users WHERE email = "$email");

The following will be helpful 以下内容会有所帮助

UPDATE US SET US.Comments = 0
FROM User_settings US,  Users U
WHERE US.Userid = U.id
AND U.email = 'emailid' 

JOIN s are slightly better in terms of performance than SUB QUERIES . JOIN的性能比SUB QUERIES更好。 Reference here . 参考这里

So instead of using this - 因此,与其使用此-

UPDATE users_settings SET commets = 0 WHERE userid
IN (SELECT id FROM users WHERE email = "$email");

I would suggest this using INNER JOIN - 我建议使用INNER JOIN

UPDATE users_settings 
SET commets = 0 
FROM user_settings 
INNER JOIN users 
on users.id = user_settings.userid   --JOIN CONDITION
WHERE users.email = "$email" ;       --SELECTION/SLICING CRITERION

Hope this helps. 希望这可以帮助。

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

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