2013-04-25 107 views
0
SELECT p.Producttype, pp.ProductID, r.Date1, r.Date2 
FROM customers AS c 
INNER JOIN orders AS r ON c.Cusid = r.Cusid 
INNER JOIN temporary AS temp ON r.Cusid = temp.Cusid 
INNER JOIN products AS pp ON pp.ProductID = temp.ProductID 
INNER JOIN Producttypes AS p ON p.Producttypeid = pp.Producttypeid 
WHERE temp.Cusid = '23' 
GROUP BY pp.ProductID 

請幫助我不知道自己能做什麼,該查詢返回每次第一行到日期1和date2我的SQL每次返回第一行,

+1

你能告訴你的表的樣本。我不明白你的問題是什麼 – usumoio 2013-04-25 00:17:12

+1

對不起,我在這裏是新的,問題是,每次查詢返回所有行中的第一行,日期列中的所有行都具有相同的值 – marina 2013-04-25 00:19:31

+0

@IamJohnGalt - 我同意他的看法。給我們一些樣本數據。 – 2013-04-25 00:20:03

回答

0

這是MySQL的一個已知的問題列。 它將允許組由沒有未分組字段的任意聚合函數。

它返回其默認行爲組,通過以外的所有領域的第一聚集。 看到這個:Why does MySQL allow "group by" queries WITHOUT aggregate functions?Any reason for GROUP BY clause without aggregation function?

刪除Group By條款,你應該沒問題。

SELECT p.Producttype, pp.ProductID, r.Date1, r.Date2 
FROM customers AS c 
INNER JOIN orders AS r ON c.Cusid = r.Cusid 
INNER JOIN temporary AS temp ON r.Cusid = temp.Cusid 
INNER JOIN products AS pp ON pp.ProductID = temp.ProductID 
INNER JOIN Producttypes AS p ON p.Producttypeid = pp.Producttypeid 
WHERE temp.Cusid = '23' 
--GROUP BY pp.ProductID -->This should be removed as it causes to returne the first of each other field while keeping ProductID distinct in the returned table.