简体   繁体   English

Propel2与表连接,然后按聚合函数分组

[英]Propel2 joining with the table and then grouping by with aggregate function

I have two tables: food(id, name), review(user_id, food_id, rating). 我有两个表:food(id,name),review(user_id,food_id,rating)。 Now I want to join food table with review table and add virtual column to the food table with name avg_rating, that will obviously hold value of average rating of the food based on the reviews. 现在我想将食物表与评论表连接起来,并在虚拟食物表中添加名称为avg_rating的虚拟列,该虚拟列显然将保留基于评论的食物的平均评分值。 So my idea was to do something like this: 所以我的想法是做这样的事情:

$food = FoodQuery::create()
        ->filterById(15) // constant for testing purposes only
        ->leftJoinWithReview()
        ->withColumn("AVG(review.rating)", "avg_rating")
        ->groupBy("review.rating")
        ->find()

Now in debugger I see this: 现在在调试器中,我看到以下内容:

result = {Propel\Runtime\Collection\ObjectCollection} [7]
 index = {array} [1]
 indexSplHash = {array} [1]
 model = "Food"
 fullyQualifiedModel = \Food
 formatter = {Propel\Runtime\Formatter\ObjectFormatter} [10]
 data = {array} [1]
  0 = {\Food} [34]
   new = false
   deleted = false
   modifiedColumns = {array} [0]
   virtualColumns = {array} [1]
    avg_rating = "5.0000"
   id = 15
   name = "Salát Caesar"
   collReviews = {Propel\Runtime\Collection\ObjectCollection} [7]
    index = {array} [2]
    indexSplHash = {array} [2]
    model = "Review"
    fullyQualifiedModel = "\Review"
    formatter = null
    data = {array} [2]
     0 = {\Review} [14]
      new = false
      deleted = false
      modifiedColumns = {array} [0]
      virtualColumns = {array} [0]
      user_id = 1
      food_id = 15
      rating = 3
      aFood = {\Food} [34]
      aUser = null
      alreadyInSave = false
      reviewThumbsUpsScheduledForDeletion = null
     1 = {\Review} [14]
      new = false
      deleted = false
      modifiedColumns = {array} [0]
      virtualColumns = {array} [0]
      user_id = 3
      food_id = 15
      rating = 5
      aFood = {\Food} [34]
      aUser = null
      alreadyInSave = false
      reviewThumbsUpsScheduledForDeletion = null
    *Propel\Runtime\Collection\Collection*pluralizer = null
   collReviewsPartial = false
   alreadyInSave = false
   reviewsScheduledForDeletion = null
 *Propel\Runtime\Collection\Collecti4

Problem is that average rating is not correct. 问题是平均评分不正确。 In virtualColumns you can see avg_rating field with value "5.0000". 在virtualColumns中,您可以看到avg_rating字段,其值为“ 5.0000”。 But when you look little bit lower you can actually see that this food has 2 reviews with rating 3 and 5, so the average should be value "4.0000". 但是,当您看上去较低时,您实际上可以看到该食物有2条评论的评分分别为3和5,因此平均值应为“ 4.0000”。

Where is the problem? 问题出在哪儿? Why is this not working correctly? 为什么这不能正常工作?

You are currently doing a left join on the review table. 您当前正在对审阅表进行左联接。 Left joining will result in several rows being returned (one per review). 左连接将导致返回几行(每条评论一个)。 For each row returned, there is only one review, so the 'average' will be the same as the review score for that row. 对于返回的每一行,只有一个评论,因此“平均”将与该行的评论分数相同。 Grouping by the average will not do much, it will only group reviews with the exact same score, and that is not what you are looking for. 按平均值分组不会做很多事情,只会将分数完全相同的评论分组,这不是您要查找的内容。

You should group by your food id first. 您应该先按食物ID分组。 Then you will be able to get a single row per food item, with an average review score. 然后,您将可以对每个食品获得一行,并获得平均评分。

$food = FoodQuery::create()
    ->filterById(15) // constant for testing purposes only
    ->groupById()
    ->leftJoinWithReview()
    ->withColumn("AVG(review.rating)", "avg_rating")
    ->find();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM