Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过查询将join留在mysql范围案例组中?_Mysql - Fatal编程技术网

如何通过查询将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字段。。。