简体   繁体   中英

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

I have table users_settings where I store settings for each user. In the following table you can see there are settings for two users 78 and 79 .

在此处输入图像描述

Now I want to add one more row each two users, key = 'app_reminder' . So is there any way to do it by query or I will have to use php script for it.

I would appreciate if someone guide me about it. Thank you

You can use 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.

If you specifically only want this for those two users, you could add where user_id in (78, 79) or use:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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