我的sql每次返回第一行,

时间:2013-04-25 00:15:53

标签: mysql sql rows

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

请帮助我不知道还能做什么,查询每次第一行返回到date1和date2列

1 个答案:

答案 0 :(得分:0)

这是MySql的已知问题。 它将允许分组,而不包含任何非分组字段的聚合函数。

它以默认行为返回group-by之外的所有字段的第一个聚合。 请参阅: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.