1

I'm building an Accounting Web Project using Nodejs and Express and I have to connect three tables from my database: parametro, tipoparametro and clasificacion.

My original query allows two tables to connect with the following code:

const parametro = await pool.query('SELECT p.parid, p.parcodigo, p.parclasificacion, tp.tipoparcodigo, p.parvalor, p.pardescripcion, p.parfechacrea FROM parametro p join tipoparametro tp WHERE p.tipoparid = tp.tipoparid ORDER BY p.parid DESC');

However, I need to add table clasificacion, but adding the second join breaks the code. Looks like this:

const parametro = await pool.query('SELECT p.parid, p.parcodigo, p.parclasificacion, c.parletras, tp.tipoparcodigo, p.parvalor, p.pardescripcion, p.parfechacrea FROM parametro p join tipoparametro tp WHERE p.tipoparid = tp.tipoparid join clasificacion c where c.parclasificacion = p.parclasificacion ORDER BY p.parid DESC');

I get the following error:

Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'join clasificacion c where c.parclasificacion = p.parclasificacion ORDER BY p.pa' at line 1

The query works fine in SQL but not inside my router.get await request.

I have searched but found no useful examples for multiple joins inside the same query for pool.query. In the future, I may even require more complex queries for my project. Is the SQL syntax incorrect, or am I missing something in order to add several tables together?

  • put the `join` before the `where` statement. There can only be one `where` statement. The reference manual refereed to by the error is [here](https://dev.mysql.com/doc/refman/8.0/en/select.html). – danblack Jul 07 '22 at 04:22
  • Turns out that there was a missing variable for certain column values that where not giving anything back from the query. The SQL was fine and yeah, there could only be one where statement. Much appreciated! – Marlon Miller Jul 09 '22 at 22:32

0 Answers0