2013-04-25 74 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.