这是琴
我目前正试图在一个特定的邮政编码通配符上平均租金。例如,如果20000的邮政编码的租金值为0,则它将插入与“20%”通配符匹配的(非零)邮政编码的平均租金。然而,目前,它正在平均所有的租金,不管通配符。我得到的是:
rent zip
2375 20000
1000 20001
2000 20002
5000 30000
1500 20003
2375 30001
但这正是我想要的:
rent zip
1500 20000
1000 20001
2000 20002
5000 30000
1500 20003
5000 30001
下面是我当前的查询:
SELECT IF(tmp.rent=0,
(select AVG(rent) from tmp where rent > 0 and zip LIKE CONCAT(substr(zip,1,2),'%')),
tmp.rent) as rent,
zip
from tmp
发布于 2018-01-11 20:50:23
对于zip的所有非空值(换句话说,“所有这些值”),这个表达式不都是TRUE吗?
zip LIKE CONCAT(substr(zip,1,2),'%'))
似乎您希望限定其中一个列引用,因此它引用了外部查询中的一个列。对所有列引用进行限定是很好的做法。
SELECT IF( t.rent=0
, ( SELECT AVG(s.rent)
FROM tmp s
WHERE s.rent > 0
AND s.zip LIKE CONCAT(SUBSTR(t.zip,1,2),'%')
)
, t.rent
) AS rent
, t.zip
FROM tmp t
https://stackoverflow.com/questions/48215471
复制相似问题