你可以用这个
CREATE VIEW myView AS
SELECT test_a.id, name, IFNULL( (
SELECT SUM( value )
FROM test_b
WHERE test_b.a_id = test_a.id
GROUP BY test_b.a_id ) , 0
) - IFNULL( (
SELECT SUM( value )
FROM test_c
WHERE test_c.a_id = test_a.id
GROUP BY test_c.a_id ) , 0
)
FROM test_a
但如果C的和大于B,这将导致负数。如果您希望绝对差大于使用它:
CREATE VIEW myView AS
SELECT test_a.id, name, ABS( IFNULL( (
SELECT SUM( value )
FROM test_b
WHERE test_b.a_id = test_a.id
GROUP BY test_b.a_id ) , 0 ) - IFNULL( (
SELECT SUM( value )
FROM test_c
WHERE test_c.a_id = test_a.id
GROUP BY test_c.a_id ) , 0
)
)
FROM test_a