MySQL - 对两个 Select 语句执行减法的正确方法 [英] MySQL - Correct Way to Perform Subtraction on Two Select Statements

查看:614
本文介绍了MySQL - 对两个 Select 语句执行减法的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个选择语句,它们返回昨天早上 7 点和今天早上 7 点的总降雨量.

I have two select statements which return a rainfall total from 7am yesterday and 7am today.

SELECT RainCounter FROM `monthly_new` WHERE LogDateTime = CURDATE() - INTERVAL 17 hour
SELECT RainCounter FROM `monthly_new` WHERE LogDateTime = CURDATE() + INTERVAL 7 hour

第一个值将始终小于或等于第二个值,但永远不会大于.

The first value will always be less than or equal to the second, but never greater.

我想弄清楚如何把它变成一个语句,我可以选择两个值,从第二个值中减去第一个值,然后返回值.

I am trying to figure out how to turn this into one statement where I can select both values, subtract the first from the second, and return the value.

推荐答案

您可以减去包装查询的 SELECT 部分中子查询的结果(如果这些子查询每个只返回一个值):

You can subtract the result of subqueries in the SELECT part of a wrapping query (if those subqueries return exactly one value each):

SELECT(子查询)-(子查询)

就你而言:

SELECT
  (SELECT RainCounter FROM `monthly_new` WHERE LogDateTime = CURDATE() + INTERVAL 7 hour) - (SELECT RainCounter FROM `monthly_new` WHERE LogDateTime = CURDATE() - INTERVAL 17 hour)

这篇关于MySQL - 对两个 Select 语句执行减法的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆