简体   繁体   English

如何将每个现有 ID 的行插入到同一个表中?

[英]How can I insert row for each existing id into same table?

I have table users_settings where I store settings for each user.我有表users_settings ,我在其中存储每个用户的设置。 In the following table you can see there are settings for two users 78 and 79 .在下表中,您可以看到两个用户7879的设置。

在此处输入图像描述

Now I want to add one more row each two users, key = 'app_reminder' .现在我想每两个用户再添加一行, key = 'app_reminder' So is there any way to do it by query or I will have to use php script for it.那么有没有办法通过查询来完成,或者我将不得不为此使用 php 脚本。

I would appreciate if someone guide me about it.如果有人指导我,我将不胜感激。 Thank you谢谢

You can use insert. . . select您可以使用insert. . . select insert. . . select insert. . . select : insert. . . select :

insert into users_settings (user_id, key)
    select distinct user_id, 'app_reminder'
    from users_settings;

Note: This assumes that id is assigned automatically.注意:这假定id是自动分配的。

If you specifically only want this for those two users, you could add where user_id in (78, 79) or use:如果您特别只想为这两个用户使用此功能,则可以where user_id in (78, 79)或使用:

insert into users_settings (user_id, key)
    select uuser_id, 'app_reminder'
    from (select 78 as user_id union all select 79) u;

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

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