Home > Net >  How to get min and max in one sql query?
How to get min and max in one sql query?

Time:12-25

what will be the query to get min and max at once I am getting errors in this query

router.get('/min/:date', (req, res) => {
  const date = req.params.date;
  console.log(typeof date);
  connection.query(
    `select max(temperature) as highesttemperature; select min(temperature) as lowesttemperature from weather_data where dt=?`,
    [date],
    (err, results, field) => {
      if (err) {
        console.log(err);
      } else {
        res.status(200).send(results);
        console.log(results);
      }
    }
  );
});

CodePudding user response:

You can have both in the same query, no need for a second select:

select max(temperature) as highesttemperature,
min(temperature) as lowesttemperature 
from weather_data 
where dt=?

The error is in your first query, there is no FROM clause.

CodePudding user response:

Try this

SELECT MIN(temperature), MAX(temperature) FROM weather_data
  • Related