求解马士兵视频里关于数据库sql语句的笔试题

问题描述:

题目在附件中!

[code="sql"]
select * from
(select a.productname,rednum-bluenum as diffnum
from (select productname,sum(num) as rednum from product where color='红色' group by productname) as a,
(select productname,sum(num) as bluenum from product where color='蓝色' group by productname) as b
where a.productname=b.productname) where diffnum>0

select a.productname as 产品,rednum as 红色,bluenum as 蓝色
from (select productname,sum(num) as rednum from product where color='红色' group by productname) as a,
(select productname,sum(num) as bluenum from product where color='蓝色' group by productname) as b
where a.productname=b.productname[/code]

[code="java"]
select * from
(
select a.productname,rednum-bluenum as diffnum

from
(
select productname,sum(num) as rednum from product where color='红色' group by productname
) as a,

(
select productname,sum(num) as bluenum from product where color='蓝色' group by productname
) as b

where a.productname=b.productname ) as t
where t.diffnum>0
[/code]

对楼上做了稍稍的修整,楼上的是正确,分可以给楼上的了。

楼上的正解,有没有考虑到性能问题呢?