简体   繁体   English

我的sql每次都会返回第一行,

[英]My sql returns the first row each time,

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

Help please i do not know what else to do, the query returns every time the first row into columns date1 and date2 帮助,我不知道该怎么办,每次将第一行输入到date1和date2列时,查询都会返回

This is a known issue of MySql. 这是MySql的已知问题。 It will allow to group by without any aggregate function of non-grouped fields. 它将允许分组依据,而没有未分组字段的任何汇总功能。

it returns the first aggregate for all fields outside the group-by in its default behavior. 它以默认行为返回分组依据之外的所有字段的第一个聚合。 see this: Why does MySQL allow "group by" queries WITHOUT aggregate functions? 看到这个: 为什么MySQL允许没有聚合功能的“分组”查询? and Any reason for GROUP BY clause without aggregation function? GROUP BY子句没有聚合功能的任何原因?

remove the Group By clause and you should be fine. 删除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.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM