Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.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
Mysql 如何从同时查询AVG的表中获取所有数据_Mysql_Sql - Fatal编程技术网

Mysql 如何从同时查询AVG的表中获取所有数据

Mysql 如何从同时查询AVG的表中获取所有数据,mysql,sql,Mysql,Sql,您好,我需要帮助解决下面的查询问题提前感谢 我有三张桌子 附件 sun\u个人 sun\u评论 我想从sun\u personal中选择用户姓名、职业和他/她的ID,然后从表附件中选择他/她的评论(每次评论和评分)和平均评分 表格:sun_个人 id|sun_id|first_name|last_name |sun_profession|sun_verified_date|sun_verified|flg ---------------------------------------------

您好,我需要帮助解决下面的查询问题提前感谢

我有三张桌子

附件
sun\u个人
sun\u评论

我想从
sun\u personal
中选择用户
姓名、职业和他/她的
ID
,然后从表
附件
中选择他/她的
评论(每次评论和评分)
平均评分

表格:sun_个人

id|sun_id|first_name|last_name |sun_profession|sun_verified_date|sun_verified|flg
---------------------------------------------------------------------------------
20|SV-001|Alex      | James    | Doctor       |2017-12-08       | 1         |1           
21|SV-002|Jane      | Rose     | Programmer   |2017-12-08       | 1         |1
表格:sun_评论

id|user_id|rev_txt   |rev_rate|rev_date  |flg                                    
----------------------------------------------------                                
1 |20     | the best | 4      |2017-12-09|1
2 |21     | know CLI | 2      |2017-12-09|1
3 |20     | recommend| 3      |2017-12-09|0
4 |20     | so far   | 3      |2017-12-09|1
表格:附件

id|user|type    |path              |flg
----------------------------------------
88|20  |passport|/upload/img128.jpg|1
89|21  |passport|/upload/img008.jpg|1
flg:1
表示该值处于活动状态,
flg:0
忽略该值

我的代码是:

SELECT 
      sun_reviews.rev_txt As txtReview, sun_reviews.rev_date As dateReview,
      sun_reviews.rev_rate As rateReview,

      AVG(sun_reviews.rev_rate) As avgREV,
      concat(sun_individual.first_name, sun_individual.last_name) As name,

      sun_individual.sun_profession As profession, 
      sun_individual.sun_verified_date As dateVerified,

      CASE when sun_verified = 1 then 'VERIFIED' else 'EXPIRED' END As status,

      attachments.path As photo 
FROM `sun_individual`

     LEFT JOIN sun_reviews ON sun_reviews.user_id = sun_individual.id
     INNER JOIN attachments ON attachments.user = sun_individual.id

WHERE attachments.type = 'passport' AND attachments.flg = 1 
      AND sun_reviews.flg = 1 AND sun_individual.flg = 1 
      AND sun_individual.sun_id LIKE '%SV-001'
我想归档的是当有人在寻找用户(比如说SV-001)时,输入代码以获得如下结果

结果:SV-001

txtReview|dateReview|rateReview|avgREV|name      |profession | dateVerified | photo
------------------------------------------------------------------------- -----------------
the best |2017-12-09|4         |3.5000|Alex James| Doctor    | 2017-12-08   |/upload/img128.jpg  
so far   |2017-12-09|3         |3.5000|Alex James| Doctor    | 2017-12-08   |/upload/img128.jpg
我希望得到与上面的类似的结果,但是当我运行查询时,我只得到一个评论

txtReview|dateReview|rateReview|avgREV|name      |profession | dateVerified | photo
------------------------------------------------------------------------- -----------------
the best |2017-12-09|4         |3.5000|Alex James| Doctor    | 2017-12-08   |/upload/img128.jpg  
我觉得有什么地方做错了。。。如果你知道我问题的解决办法,请帮助我

谢谢

select
  a.rev_txt                    as txtReview,
  a.rev_date                   as dateReview,
  a.rev_rate                   as rateReview,
  d.avgRev                     as avgRev,
  b.first_name || b.last_name  as name,
  b.sun_profession             as profession, 
  b.sun_verified_date          as dateVerified,
  case
    when sun_verified = 1 then 'VERIFIED' 
    else 'EXPIRED' 
  end                          as status,
  c.path                       as photo
from
  sun_reviews a
  join
  sun_individual b
  on a.user_id = b.id
  join
  attachments c
  on c.user_id = b.id
  join
  (
    select 
      user_id,
      avg(rev_rate) as avgRev
    from
      sun_reviews
    where
      flg = 1
    group by 
      user_id
  ) d
  on d.user_id = b.id
where
  c.type = 'passport' and
  c.flg = 1 and
  a.flg = 1 and 
  b.flg = 1 and
  b.sun_id LIKE '%SV-001';

 txtreview | datereview | ratereview |       avgrev       |   name    | profession | dateverified |  status  |       photo        
-----------+------------+------------+--------------------+-----------+------------+--------------+----------+--------------------
 the best  | 2017-12-09 |          4 | 3.5000000000000000 | AlexJames | Doctor     | 2017-12-08   | VERIFIED | /upload/img128.jpg
 so far    | 2017-12-09 |          3 | 3.5000000000000000 | AlexJames | Doctor     | 2017-12-08   | VERIFIED | /upload/img128.jpg
(2 rows)

如果有group by,则结果集中的所有列都必须是group by columns或AGGRATES。我猜您使用的是MySQL,它不强制执行这一点。不过,您应该自己强制执行,否则会变得非常混乱。

查询非常有效,谢谢。我还将研究“你应该自己执行”。非常感谢。