简体   繁体   中英

How to get min and max in one sql query?

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);
      }
    }
  );
});

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.

Try this

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM