更改@bluefeet 的查询,因此没有派生表。它可能会在某些 MySQL 版本中提高效率,但会破坏 ANSI/ISO SQL 的有效性,并且不会以ONLY_FULL_GROUP_BY模式运行:
select brand,
max(case when attr='RAM' then cast(vals as unsigned) end) as RAM,
max(case when attr='CPU' then cast(vals as unsigned) end) as CPU
from attributes
group by brand
having RAM > 500
and CPU > 1300 ;
改进它,所以它也是有效的:
select brand,
max(case when attr='RAM' then cast(vals as unsigned) end) as RAM,
max(case when attr='CPU' then cast(vals as unsigned) end) as CPU
from attributes
group by brand
having max(case when attr='RAM' then cast(vals as unsigned) end) > 500
and max(case when attr='CPU' then cast(vals as unsigned) end) > 1300 ;