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列
答案 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.