如何 Select 来自两个不同 table(A,B) 的列,其中 sum(column tableA) 小于 columntableB
How to Select columns from tow different table(A,B) where sum(column tableA) less then columntableB
我可以 select 总和(税)低于价格的价格,我正在执行查询但出现错误
select b.price
from tableA a
inner join tableB b on b.idprice = a.idprice where sum(tax)<price
group by a.idprice,b.price
所以像这样:
SELECT t.*,s.*
FROM TableA t
INNER JOIN(SELECT idprice,sum(tax) as sum_tax
FROM TableB
GROUP BY idprice) s
ON(t.idprice = s.idprice and t.price < s.sum_tax)
或者如果价格 table 也应该相加:
SELECT t.idprice,sum(t.price),max(s.sum_tax)
FROM TableA t
INNER JOIN(SELECT idprice,sum(tax) as sum_tax
FROM TableB
GROUP BY idprice) s
ON(t.idprice = s.idprice)
GROUP BY t.idprice
having sum(t.price) < max(s.sum_tax)
我认为您原来的方法可以通过使用 having
子句而不是 where
:
select b.price
from tableA a inner join
tableB b
on b.idprice = a.idprice
group by a.idprice, b.price
having sum(tax) < b.price;
我可以 select 总和(税)低于价格的价格,我正在执行查询但出现错误
select b.price
from tableA a
inner join tableB b on b.idprice = a.idprice where sum(tax)<price
group by a.idprice,b.price
所以像这样:
SELECT t.*,s.*
FROM TableA t
INNER JOIN(SELECT idprice,sum(tax) as sum_tax
FROM TableB
GROUP BY idprice) s
ON(t.idprice = s.idprice and t.price < s.sum_tax)
或者如果价格 table 也应该相加:
SELECT t.idprice,sum(t.price),max(s.sum_tax)
FROM TableA t
INNER JOIN(SELECT idprice,sum(tax) as sum_tax
FROM TableB
GROUP BY idprice) s
ON(t.idprice = s.idprice)
GROUP BY t.idprice
having sum(t.price) < max(s.sum_tax)
我认为您原来的方法可以通过使用 having
子句而不是 where
:
select b.price
from tableA a inner join
tableB b
on b.idprice = a.idprice
group by a.idprice, b.price
having sum(tax) < b.price;