Search code examples
postgresqlnode-postgrespgpool

node-postgres LEFT JOIN query


I am attempting to effect a join query in node-postgres (pg) with the following function but am getting a syntax error. The problem is the join query, everything else works fine. What is the correct way to format a join query in pg?

exports.bigBook = function(req, res) {
  var bookNumber = req.params.id;
  pool.connect(function(err, client, done) {
   if (err) { return console.error('error fetching client from pool', err);}
   client.query('SELECT * FROM book WHERE id = $1 LEFT JOIN author 
    ON (book.author = author.auth_id)'), [bookNumber], function (err, results) {
    client.release();
    res.send(results.rows);
  };
 })
}

Solution

  • The LEFT JOIN is part of the FROM clause, so you'll have to move the WHERE clause to the end of the query.