Zubair Mushtaq Zubair Mushtaq - 4 months ago 8
SQL Question

How to insert the values in one table by getting the id from other table

I want to add a seprate popup for every user from the tbl_users in the tbl_pop_up_messages. For example, If I have 50 users in tbl_users, 50 rows should be added in the tbl_pop_up_messages against every user.S that, every logged in user will see the popup and the popup will be deactivated after the clic.My conern is only to add the rows. Kindly, guide.

SELECT user_id FROMt bl_users;
INSERT INTO tbl_pop_up_messages (user_id, popup_text, active)
VALUES ('3', 'Pop Up Text', '1');

Answer

You can do this as:

INSERT INTO tbl_pop_up_messages(user_id, popup_text, active) 
    SELECT  u.user_id, 'Pop Up Text', '1'
    FROM tbl_users u;