【问题标题】:SELECT column but also add AVG columnSELECT 列但也添加 AVG 列
【发布时间】:2016-06-21 10:41:31
【问题描述】:

我不太确定如何扩展此查询,以便我也可以拥有一个 AVG(price_current) 列...而不必在查询返回后在 PHP 中计算此值。

SELECT
  listing_subtype, bedrooms, total_baths, tot_sqft_finished, price_current, latitude, longitude, (
    3959 * acos (
      cos ( radians(48.639) )
      * cos( radians( latitude ) )
      * cos( radians( longitude ) - radians(-123.404) )
      + sin ( radians(48.639) )
      * sin( radians( latitude ) )
    )
  ) AS distance
FROM rets_property_resi
WHERE listing_subtype = 'Single Family Detached' AND
bedrooms >= 2 AND bedrooms <= 3 AND
total_baths >= 1 AND total_baths <= 2 AND
tot_sqft_finished >= 2000 AND tot_sqft_finished <= 2500
HAVING distance < 5
ORDER BY distance
LIMIT 0, 25; 

【问题讨论】:

  • 您想要对所有返回的结果进行平均,并且所有返回的行都具有相同的值,还是想要对某些选定的值进行平均,例如:浴室等?

标签: mysql sql select average resultset


【解决方案1】:

我在这里做一些假设:

  1. 您在请求的房屋周围的给定半径内寻找类似的房屋/房屋。这些房屋中的每一个在您要查询的表中都有一个主键。让我们在这里称呼它为primKey

  2. 此外,您需要限制为 0.25 的所有返还房屋的平均价格,而不是表中与 WHERE 子句匹配的所有记录的 AVG。

因此,所有返回的行都会有一个带有平均价格的额外字段,并且对于每一行都是相同的。

您必须再次运行相同的查询作为子查询。但是,由于您想限制 AVG,您必须在子查询中运行它以计算正确的 AVG。 这真的很笨重:

SELECT
  rets_property_resi.listing_subtype, rets_property_resi.bedrooms, rets_property_resi.total_baths, rets_property_resi.tot_sqft_finished, rets_property_resi.price_current, rets_property_resi.latitude, rets_property_resi.longitude, (
    3959 * acos (
      cos ( radians(48.639) )
      * cos( radians( latitude ) )
      * cos( radians( longitude ) - radians(-123.404) )
      + sin ( radians(48.639) )
      * sin( radians( latitude ) )
    )
  ) AS distance, outerSubQuery.averagePrice
FROM rets_property_resi
LEFT JOIN
(
    SELECT innerSubQuery.primKey AS primKey, AVG(innerSubQuery.price_current) AS averagePrice
    FROM
        (SELECT
          primKey, price_current, (
            3959 * acos (
              cos ( radians(48.639) )
              * cos( radians( latitude ) )
              * cos( radians( longitude ) - radians(-123.404) )
              + sin ( radians(48.639) )
              * sin( radians( latitude ) )
            )
          ) AS distance
        FROM rets_property_resi
        WHERE listing_subtype = 'Single Family Detached' AND
        bedrooms >= 2 AND bedrooms <= 3 AND
        total_baths >= 1 AND total_baths <= 2 AND
        tot_sqft_finished >= 2000 AND tot_sqft_finished <= 2500
        HAVING distance < 5
        ORDER BY distance
        LIMIT 0, 25) AS innerSubQuery
    GROUP BY innerSubQuery.primKey
) AS outerSubQuery ON (outerSubQuery.primKey = rets_property_resi.primKey)

WHERE listing_subtype = 'Single Family Detached' AND
bedrooms >= 2 AND bedrooms <= 3 AND
total_baths >= 1 AND total_baths <= 2 AND
tot_sqft_finished >= 2000 AND tot_sqft_finished <= 2500
HAVING distance < 5
ORDER BY distance
LIMIT 0, 25; 

我敢打赌,这有一个更优雅的版本。您最好在此处使用临时表或继续在代码中进行计算。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-20
    • 1970-01-01
    • 2020-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多