KMA Badshah
KMA Badshah

Reputation: 1115

SELECT statements as INSERT parameters along with other parameters

Both of the SELECT statements return only one scalar data each. I wish to pass them as the first two parameters of the INSERT statement. But it's throwing an error stating invalid comma after the second SELECT statement. What am I doing wrong here?

INSERT INTO messages(src_id, tar_id, body, time)
         SELECT id FROM chatters WHERE (uname = 'adnan'),
       SELECT id FROM chatters WHERE (uname = 'john'), 'Hello John', '1971-12-16';

Upvotes: 0

Views: 53

Answers (2)

Charlieface
Charlieface

Reputation: 71169

Put the whole SELECT in brackets:

INSERT INTO messages(src_id, tar_id, body, time)
   VALUES
  ( ( SELECT id FROM chatters WHERE (uname = 'adnan') ),
   ( SELECT id FROM chatters WHERE (uname = 'john') ),
   'Hello John',
   '1971-12-16' );

Upvotes: 1

Popeye
Popeye

Reputation: 35900

You can use the insert into .. select as follows:

INSERT INTO messages(src_id, tar_id, body, time)
   SELECT max(case when uname = 'adnan' then id end),
          max(case when uname = 'john' then id end),
          'Hello John',
          '1971-12-16'
     FROM chatters 
    where uname in ('adnan','john');

Upvotes: 0

Related Questions