简体   繁体   English

基于第二个表字段值的MySQL海量表更新

[英]MySQL mass table update based on second table field values

I have 2 tables, one for user_data and one for user_stats. 我有2个表,一个用于user_data,一个用于user_stats。 Im trying to update a field in user_data for all users to a single value ("active") based on the last login field of the user_stats table. 我试图根据user_stats表的最后一个登录字段将所有用户的user_data中的字段更新为单个值(“活动”)。

Im trying to avoid a loop since all corresponding fields will just be updated to "active". 我试图避免循环,因为所有对应的字段都将被更新为“活动”。

Heres basically what Im trying to do: UPDATE user_data SET status = 'active' WHERE user_stats.login <= '$last_login' 这基本上是我想做的:UPDATE user_data SET status ='active'WHERE user_stats.login <='$ last_login'

Something like the following join should work: 像下面的联接这样的东西应该起作用:

UPDATE user_data, user_stats
SET user_data.status='active'
WHERE user_stats.user_id=user_data.user_id
AND user_stats.last_login <= '$lastlogin'

You may need to change the fields used to join the tables in the update query to suit your tables. 您可能需要在更新查询中更改用于联接表的字段以适合您的表。

You'll have to nest your query then: 然后,您必须嵌套查询:

UPDATE user_data SET status = 'active' WHERE user_id IN (SELECT id FROM user_stats WHERE login <= '$last_login') UPDATE user_data SET status ='active'WHERE user_id IN(SELECT id from user_stats WHERE login <='$ last_login')

Overall I'd recommend considering a whole different architecture 总体而言,我建议您考虑使用完全不同的体系结构

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

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