Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

151
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda