例如,我有以下表格:
我想从
我尝试了以下SQL查询语句,但如果一个日期上没有b的记录,它会返回
date | id | num
01-01 | a | 10
01-02 | a | 14
01-02 | b | 2
01-03 | a | 19
01-03 | b | 5
01-04 | a | 13
我想从
a
中减去b
的num
,即结果将为:01-01 | 10 //10
01-02 | 12 //14-2
01-03 | 14 //19-5
01-04 | 13 //13
我尝试了以下SQL查询语句,但如果一个日期上没有b的记录,它会返回
\N
。SELECT tba.date, numall-numout
FROM (
SELECT date, num AS numall
FROM tb
WHERE id = "a"
) tba
LEFT JOIN (
SELECT date, num AS numout
FROM tb
WHERE id = "b"
) tbb
ON tba.date = tbb.date