How to concatenate a value with the ORDER BY? - Nodejs

1

What would be the correct way to concatenate the value that I have in the constant data in my MySQL query? The way I'm doing does not work.

const data = req.body;

req.getConnection((err, conn) => {
    conn.query('SELECT * FROM ordenar_usuario ORDER BY ', data, (err, usuarios) => {
        if (err) {
            res.json(err);
        }
        res.render('ordenar', {
            data: usuarios
        });
    });
    
asked by Tiago de Oliveira 02.11.2018 в 19:40
source

2 answers

0

I built an example that I hope will be useful, where only concatenate the value of a variable that I called id to pass it to the query

The query I put it in quotation marks "" and at the end said variable only concatenates it like this: +id

EXAMPLE

const express = require('express')
const app = express()
const mysql = require('mysql')

let connection = mysql.createConnection({
    'server': 'localhost',
    'user': 'root',
    'password': 'secret',
    'port': '3307',
    'database': 'data'
})

connection.connect()

app.get('/all', function(req, res){
    let id = 1
    connection.query("SELECT * FROM users WHERE id = "+id, function(errors, results, fields){
        console.log(results)
    })
})

app.listen('8000', () => {
    console.log("running")
})

At the end of the console, he throws me the following

[ RowDataPacket {
    id: 1,
    nameUser: 'gatito',
    passwordUser: 'password',
    statusUser: 1,
    created_at: 2018-10-10T14:40:14.000Z } ]
    
answered by 02.11.2018 в 20:31
-1

Friend does not answer your question correctly, but with the fetch function I use something similar and using the same logic, it would work like this: 'SELECT * FROM ordenar_usuario ORDER BY ' + data + ''

Luck

    
answered by 02.11.2018 в 20:16