Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

152
Vistas
ORDER BY is not working when I try to use this psql query

I have a psql database storing reviews for a certain product.
When the client requests data from the api, it sends in a request with a product_id, count, and a sort as query parameters.

I have this snippit of code below that should query the database and bring back the correct data.

async (query) => {
const sort = {
  relevance: 'helpfulness DESC, date DESC',
  helpfulness: 'helpfulness DESC',
  newest: 'date DESC'
};

const sql = 'SELECT id, product_id, rating, to_timestamp(date / 1000) AS date, summary, body, recommend, reviewer_name, reviewer_email, response, helpfulness, photos FROM reviews WHERE product_id=$1 AND reported=false ORDER BY $2 LIMIT $3'

try {
  const result = await pool.query(sql ,[query.product_id, sort[query.sort] || sort.relevance, query.count || 5]);
  return result;
} catch (error) {
  throw new Error(error);
}}

My issue is that the sort does not apply at all during the query. It will grab the right product_id and also limit the rows that are returned properly but the sort just won't apply.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda