SQL - 如何计算列值并与另一个表连接

时间:2014-07-29 12:07:39

标签: mysql sql sum

由于我不熟悉MySQL查询,所以我希望有人帮我创建这种sql查询。

我有两个MySQL表,如下所示:

Table Name: rating
-------------------
property_id   user_id   area_rate_count   safety_rate_count   friendly_rate_count   walkability_rate_count
4             28        1                 1                   1                     2
5             38        2                 3                   4                     1
5             40        2                 2                   3                     1
6             40        2                 3                   1                     4
10            43        2                 2                   3                     1  

Table Name: listing
-------------------
property_id   title
4             Sample 1
5             Sample 2
6             Sample 3
10            Sample 4
11            Sample 5
12            Sample 6

现在我首先想要对每一列进行求和并进行除法。 (area_rate_count,safety_rate_count,friendly_rate_count,walkability_rate_count)。例如,在property_id:5中有两次,所以首先计算列总和并除以2.

经过计算,我们将获得此输出:

Table Name: rating (After Calculation)
--------------------------------------
property_id   rate
4             5
5             9 (Divided by 2 because this property_id is two times in table)
6             10
10            8

最后,我希望将此结果加入到我的listing表中,结果看起来像这样:

Table Name: listing
-------------------
property_id   title         rate
4             Sample 1      5
5             Sample 2      9 (Divided by 2 becouse property_id is two times in table)
6             Sample 3      10
10            Sample 4      8
11            Sample 5      0
12            Sample 6      0

感谢。

6 个答案:

答案 0 :(得分:1)

我认为您需要avg()聚合函数以及join

select l.property_id, l.title,
       coalesce(avg(area_rate_count + safety_rate_count + friendly_rate_count + walkability_rate_count
          ), 0) as rate
from listing l left outer join
     property_id p
     on l.property_id = p.property_id
group by l.property_id, l.title ;

答案 1 :(得分:1)

如果我理解正确,我认为你需要这个:

select l.property_id, l.title, coalesce(r.ssum/if(r.ct=0,1,r.ct), 0) as rate
  from listing l LEFT JOIN
       (select property_id,
               sum(area_rate_count+safety_rate_count
                     +friendly_rate_count+walkability_rate_count) ssum,
               count(*) ct
          from rating 
         group by property_id ) r 
       ON l.property_id = r.property_id
  order by l.property_id

在这里查看小提琴:http://sqlfiddle.com/#!2/589d6/5

修改

OP在评论中询问他希望listing这里的所有列都是他想要的:

select l.*, coalesce(r.ssum/if(r.ct=0,1,r.ct), 0) as rate
  from listing l LEFT JOIN
       (select property_id,
               sum(area_rate_count+safety_rate_count
                     +friendly_rate_count+walkability_rate_count) ssum,
               count(*) ct
          from rating 
         group by property_id ) r 
       ON l.property_id = r.property_id
  order by l.property_id

答案 2 :(得分:0)

CREATE TEMPORARY TABLE IF NOT EXISTS 
  temp_table ( INDEX(col_2) ) 
ENGINE=MyISAM 
 AS (
    SELECT
    property_id,
    AVG(area_rate_count) as area_rate_count, 
    AVG(safety_rate_count) as safety_rate_count, 
    AVG(friendly_rate_count) as friendly_rate_count, 
    AVG(walkability_rate_count) as walkability_rate_count
    FROM rating
    GROUP BY property_id
)

SELECT * FROM listing L
JOIN temp_table T
ON L.property_id = T.property_id

答案 3 :(得分:0)

使用以下语句以不同的速率获取不同的property_id

 select property_id, sum(separaterating)/count(property_id) from (
select property_id,sum(area_rate_count  , safety_rate_count  , friendly_rate_count  , walkability_rate_count) as separaterating from rating group by property_id  AS temp ) group by

PROPERTY_ID

然后您可以与其他表联接以获得最终结果,如下所示

 select * from (  select property_id, sum(separaterating)/count(property_id) from (
select property_id,sum(area_rate_count  , safety_rate_count  , friendly_rate_count  , walkability_rate_count) as separaterating from rating group by property_id  AS temp ) group by

property_id)AS在A.property_id = B.property_id上列出AS B的内部联接

答案 4 :(得分:0)

试试这个:

    select a.prop_id as property_id,  l.title,  a.allratings / b.numberofreviews as rate
    from 
    (
        select property_id as prop_id, SUM(coalesce(area_rate_count,0)  + coalesce(safety_rate_count,0) + coalesce(friendly_rate_count,0) + coalesce(walkability_rate_count,0))  as allratings
        from rating
        group by property_id
        ) a inner join 
          (
            select property_id, count(distinct user_id) as numberofreviews
            from rating
            group by property_id
        ) b on a.property_id = b.property_id
        inner join listing  l on a.property_id = l.property_id

答案 5 :(得分:0)

尝试此查询

select ls.property_id,ls.title,inr.rate from listing as ls left join (select r.property_id as pid,r.rate/r.cnt as rate from (select property_id,user_id,(area_rate_count+safefty_rate_count+friendly_rate_count+walkability_rate_count) as rate,count(*) as cnt from rating group by property_id) as r) as inr on inr.pid=ls.property_id