select sum(数量) from 表M a where exists(select 1 from 表N b where a.东西=b.东西 and a.用户名=b.用户名 and a.卖出价格>b.买进价格)
and 用户名='a'