如何通过查询将join留在mysql范围案例组中?
如何将平均值加入此查询如何通过查询将join留在mysql范围案例组中?,mysql,Mysql,如何将平均值加入此查询 select t.range, count(*) as num from (select case when price < 50000 then '0 - 49K' when price >= 50000 and price < 100000 then '50 - 99K' when price >= 100000 and price < 200000 then '100 - 199K'
select t.range, count(*) as num
from
(select case
when price < 50000 then '0 - 49K'
when price >= 50000 and price < 100000 then '50 - 99K'
when price >= 100000 and price < 200000 then '100 - 199K'
...
as range,
price
from table) as t
group by range
我试过了
select t.range, count(*) as num, avg(b.val)
from
(select case
when price < 50000 then '0 - 49K'
when price >= 50000 and price < 100000 then '50 - 99K'
when price >= 100000 and price < 200000 then '100 - 199K'
...
as range,
price
from table) as t
left join table2 b on b.id = t.id
group by range
和其他各种微弱的尝试都没有用。如果不知道table2是什么以及它与table的关系,很难说清楚,但我的第一个想法是在子查询内部而不是外部执行联接:
select t.range, count(*) as num, avg(t.val)
from
(select case
when price < 50000 then '0 - 49K'
when price >= 50000 and price < 100000 then '50 - 99K'
when price >= 100000 and price < 200000 then '100 - 199K'
...
as range,
t1.price,
b.val
from table t1
left join table2 b on b.id = t1.id
) as t
group by range
也许我错了,现在很晚了,但我在t定义中没有看到id字段。。。