This is an example of a query I've tried:
const order = req.params.order connection.query('select * from students order by first_name key=?', order, (err)=>{... });
By default without specifying the way, I retrieve the information ordered by first_name asc.
I would like the data ordered by asc or desc according what has been sent through the params.
Im assuming that req.params. order = ASC or DESC. So try this:
connection.query(`select * from students order by fisrt_name ${order}`, order, (err)=>{
...
});
or
const myQuery = `select * from students order by fisrt_name ${order}`;
connection.query(myQuery, order, (err)=>{
...
});
Do this. If order is undefined it will order by ASC by default. If it is defined, it will order by the parameter. I hope this is what you were looking for.
connection.query(
`select * from students order by first_name ${order ? order : 'ASC'}`
, order, (err)=>{
...
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.