有人已經幫我解決這個查詢,但我做了一個調整,我得到了一個問題:SQL有隻得到第一個記錄的行表,我想所有
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 = "[email protected]"
當tyd.login = "[email protected]"
這是它只能較低的ID。不適用於user1或user2 ...我只是不明白爲什麼......!此外,它通過tyd.login但在這種情況下,AVG和COUNT函數只工作在一行中工作,如果我組...
下面是表:
id id_marchand id_product login price amount delay step time
29 1 1 [email protected] 344 1 0 1 1343297500
120 1 1 [email protected] 54 1 0 1 1343297504
109 1 1 [email protected] 34 1 0 1 1343298598
當HAVING tyd.login = "[email protected]"
它完美的作品。當user1或user2我有0行。
感謝您的幫助
的第一個主題:Sql, get the average on a group while escaping the where condition
您在濫用'HAVING',通常用於組過濾或聚合函數。我想你應該使用'WHERE tyd.login =「[email protected]」'。另外,您沒有「user1」,但是沒有「user10」。 – 2012-07-31 16:45:29
如果我不使用having子句,則只會獲得userX行的AVG和COUNT。我希望每一行的AVG和COUNT都是相同的id_product和id_marchand。對不起,用戶1和10,我糾正它。 – pierreaurelemartin 2012-07-31 16:50:15
你究竟想從這個查詢中得到什麼?和你是什麼意思**當HAVING tyd.login =「[email protected]」它完美的工作。**。你能解釋更多嗎? – hmmftg 2012-07-31 17:36:20