0

I am trying to do a batch of insert to mysql. I got this code to work without the inner select question. How do I do a bulk insert in mySQL using node.js But when i add the select i get "ER_PARSE_ERROR".

var arr = [[1, '123456789', '2000-01-01', 1],[2, '123456789', '2000-01-02', 1],[3, '123456789', '2000-01-03', 1]];

objConn.query("INSERT INTO stats (`name`, `phone`, `dayOfCall` , `calls`) \
                VALUES ((SELECT `name` as myname FROM `contact` WHERE `id` = ? \
                LIMIT 1), ?, ?, ?)",
            [arr],
            function (err, row, fields){
                if(err)
                    logger.info(err);
            }
        );
Community
  • 1
  • 1
Krister Johansson
  • 691
  • 2
  • 10
  • 34
  • The forms are `INSERT .... VALUES (), (), ();` or `INSERT ... SELECT ...;` NOT `INSERT ... VALUES ((SELECT...` – Uueerdo Dec 02 '15 at 00:08
  • @Uueerdo write it up – Drew Dec 02 '15 at 00:13
  • Yes please, would it work if the select has a WHERE id = ? to it? – Krister Johansson Dec 02 '15 at 00:15
  • @Drew I would, but trying to keep focused on other matters right now. – Uueerdo Dec 02 '15 at 00:35
  • 1
    @KristerJohansson Yes, the WHERE and even the LIMIT should still work as is; though I am still wondering where you are expecting the `phone`, `day`, and even `call` values to come from. – Uueerdo Dec 02 '15 at 00:37
  • also, the data you insert has to clash/trigger/violate (whatever you want to call it) ... a unique key, so that your IODKU works. If you don't have that under your belt, I can point you to one of my answers. In fact, here is a link [Right Here](http://stackoverflow.com/a/32468519). It does an increment on a column to show it works. "It works" meaning a new row is not inserted, rather updated, and proves it by incrementing a column value on that row. – Drew Dec 02 '15 at 00:50
  • So, write a question at 2am is not werry good :) Fix the sql some more, but the problem still remains. – Krister Johansson Dec 02 '15 at 07:33

0 Answers0