4

请原谅我的英语,但没有人能用法语回答;-)

我正在做这个请求:

SELECT AVG(tyd.price) AS avg_price, COUNT(tyd.id_product) AS cnt_id_p, 
       catalog.id_marchand, catalog.id_product, catalog.price AS c_price,
       catalog.img_src, tyd.login AS tyd_l
  FROM catalog 
 INNER JOIN tyd ON catalog.id_marchand = tyd.id_marchand 
               AND catalog.id_product = tyd.id_product
 WHERE tyd.login = "user1@tyd.fr"
   AND tyd.step = "0"
 GROUP BY catalog.id_product, catalog.id_marchand

但是当然 AVG & COUNT 函数不起作用,因为我的条件只有一行。

我想做的仍然是获得我独特的线路,但让 AVG 和 COUNT 功能正常工作。我可以用两个查询来做,但我更喜欢做一个。

我希望你能帮助我。

谢谢。

PS:我在答案3中提出了另一个问题。我生气了!

4

3 回答 3

0

尝试

SELECT 
    AVG(tyd.price) AS avg_price, COUNT(tyd.id_product) AS cnt_id_p,
    catalog.id_marchand, catalog.id_product, catalog.price AS c_price, 
    catalog.img_src, tyd.login AS tyd_l
FROM catalog 
INNER JOIN tyd ON catalog.id_marchand = tyd.id_marchand 
              AND catalog.id_product =   tyd.id_product
              AND tyd.step = "0" 
GROUP BY catalog.id_product, catalog.id_marchand
HAVING tyd.login = "user1@tyd.fr"
于 2012-07-31T12:50:11.650 回答
0

您应该在 GROUP BY 子句中包含所有不属于聚合函数的列。

SELECT AVG(tyd.price) AS avg_price, COUNT(tyd.id_product) AS cnt_id_p,  
       catalog.id_marchand, catalog.id_product, catalog.price AS c_price, 
       catalog.img_src, tyd.login AS tyd_l 
  FROM catalog  
 INNER JOIN tyd ON catalog.id_marchand = tyd.id_marchand  
               AND catalog.id_product = tyd.id_product 
 WHERE tyd.login = "user1@tyd.fr" 
   AND tyd.step = "0" 
 GROUP BY catalog.id_marchand, catalog.id_product, catalog.price AS c_price, 
       catalog.img_src, tyd.login 
于 2012-07-31T13:06:26.947 回答
0

很抱歉再问,但我使用相同的请求加入另一个表,如下所示:

    SELECT AVG(tyd.price) AS avg_price, COUNT(tyd.id_product) AS cnt, tyd.id_marchand, tyd.id_product, catalog.price AS c_price, tyd.price AS t_price, tyd.amount AS t_am, pro_tyd.amount AS p_am, pro_tyd.price AS p_price, catalog.img_src,  tyd.step, tyd.login AS tyd_l
    FROM catalog 
    INNER JOIN tyd
    ON catalog.id_marchand = tyd.id_marchand 
    AND catalog.id_product =   tyd.id_product
    AND tyd.step = "1" 
    INNER JOIN pro_tyd 
    ON tyd.id_marchand = pro_tyd.id_marchand 
    AND tyd.id_product = pro_tyd.id_product
    GROUP BY catalog.id_product, catalog.id_marchand
    HAVING tyd.login = "user1@tyd.fr"

并且它仅在 tyd.login = "user3@tyd.fr" 这是较低的 id 时有效。不适用于 user1 或 user2...我只是不知道为什么...!

这是表格:

id / id_marchand / id_product / login / price / amount / delay / step / time

29 / 1 / 1 / user3@tyd.fr / 344 / 1 / 0 / 1 / 1343297500

120 / 1 / 1 /user2@tyd.fr / 54 / 1 / 0 / 1 / 1343297504

109 / 1 / 1 / user10@tyd.fr / 34 / 1 / 0 / 1 / 1343298598

当 HAVING tyd.login = "user3@tyd.fr" 它工作得很好。当 user1 或 user2 我得到 0 行。

于 2012-07-31T14:27:31.930 回答