如果使用AVG函数,SQL查询结果将减少为单个结果

时间:2016-04-06 11:12:10

标签: mysql sql database symfony doctrine-orm

我有这个查询,我试图从各种不同的供应商获取我的所有包。我想要做的是:对于每个包,我想要放置包所属供应商的平均评级。

SELECT 
    p.id as package_id,
    p.title as package_title,
    p.path as package_image,
    p.packageQuantity as package_quantity,
    p.price as package_price, 
    AVG(r.star_rating) as star_rating 
FROM 
    package as p 
INNER JOIN vendor as v ON v.id = p.vendor_id 
INNER JOIN review as r ON r.vendor = v.id

上述查询的结果:

Array
(
    [0] => Array
        (
            [package_id] => 3
            [package_title] => Simple
            [package_image] => 1f496761e41e3b23d014f5511d591664c94e6390.jpeg
            [package_quantity] => 120 Minutes
            [package_price] => 1236
            [star_rating] => 3.3333333333333335
        )

)

问题在于,通过上述查询,与删除AVG列时获得的所有结果相比,我只获得了一个结果。

SELECT 
    p.id as package_id,
    p.title as package_title,
    p.path as package_image,
    p.packageQuantity as package_quantity,
    p.price as package_price
FROM 
    package as p 
INNER JOIN vendor as v ON v.id = p.vendor_id 

没有AVG的查询结果:

Array
(
    [0] => Array
        (
            [package_id] => 1
            [package_title] => Normal
            [package_image] => 38fbe34ff595a282a171c6058f40f676bde91665.jpeg
            [package_quantity] => 120 Photos
            [package_price] => 1200
        )
[1] => Array
    (
        [package_id] => 2
        [package_title] => Medium Package(Ui)
        [package_image] => 5c9a34123d7e18237641355c59e91b10570e0fed.jpeg
        [package_quantity] => 250 Photos
        [package_price] => 2400
    )

[2] => Array
    (
        [package_id] => 3
        [package_title] => Simple
        [package_image] => 1f496761e41e3b23d014f5511d591664c94e6390.jpeg
        [package_quantity] => 120 Minutes
        [package_price] => 1236
    )

[3] => Array
    (
        [package_id] => 4
        [package_title] => Better Package
        [package_image] => d0ab8bee318da6b3f1d515835cadcc8669afc0f2.jpeg
        [package_quantity] => 250 Minutes
        [package_price] => 2366
    )

[4] => Array
    (
        [package_id] => 5
        [package_title] => Advance Package
        [package_image] => 532b67366f327a17a4cb3e5b194d52950dd58a9b.jpeg
        [package_quantity] => 600 Minutes
        [package_price] => 5698
    )

[5] => Array
    (
        [package_id] => 6
        [package_title] => Excellent Package
        [package_image] => 6b7272f9ae03229e41bde2e5933e42b4db061c70.jpeg
        [package_quantity] => 1000 Minutes
        [package_price] => 8966
    )

我需要能够获取所有软件包并显示其供应商的平均评级,即使它是重复的。

2 个答案:

答案 0 :(得分:2)

我认为这是子查询的好机会:

SELECT 
    p.id as package_id,
    p.title as package_title,
    p.path as package_image,
    p.packageQuantity as package_quantity,
    p.price as package_price, 
    (SELECT AVG(r.star_rating) 
     FROM vendor v INNER JOIN
          review r 
          ON r.vendor = v.id
     WHERE  v.id = p.vendor_id
    ) as star_rating 
FROM package p;

答案 1 :(得分:2)

AVG是聚合函数,因此您应该定义GROUP BY列:

SELECT 
    p.id as package_id,
    p.title as package_title,
    p.path as package_image,
    p.packageQuantity as package_quantity,
    p.price as package_price, 
    AVG(r.star_rating) as star_rating 
FROM 
    package as p 
INNER JOIN vendor as v ON v.id = p.vendor_id 
INNER JOIN review as r ON r.vendor = v.id
GROUP BY p.id