Home > Blockchain >  how can i have select inside insert into?
how can i have select inside insert into?

Time:06-01

This is the Mysql im trying to run but its not working

INSERT into agent_daily_stats (date,login_id,emails_handled,chats_handled) 
VALUES (
  '5/1/2022 12:00:00 AM',
   SELECT login_id FROM ic_agent where login_name = rani_azzam,
  2,
  2
)

CodePudding user response:

Making a few assumptions with a valid date and string literal for the login_name and that you only want to insert if the row exists, you can simply include all your columns as a select

INSERT into agent_daily_stats (date, login_id, emails_handled, chats_handled) 
SELECT '2022-05-01 00:00:00', login_id, 2, 2
FROM ic_agent 
WHERE login_name = 'rani_azzam';

CodePudding user response:

If there are more inserts with different login_names then query can be modified as below.

INSERT into agent_daily_stats (date,login_id,emails_handled,chats_handled) 
VALUES ('2022-05-01 00:00:00',(SELECT login_id from loginid where login_name = rani_azzam),2,2)

Same can be viewed on db<>fiddle<>example

  • Related