As has been mentioned it is worth switching to the newer ANSI 92 JOIN syntax for clarity, this can avoid accidental cross joins, so your query would become:
SELECT o.Date1, o.Date2
FROM customers AS c
INNER JOIN orders AS o
ON c.custid = o.custid
INNER JOIN temptable AS te
o.custid = te.custid
INNER JOIN products AS p
ON pi.prodno = te.prodno
INNER JOIN products_ids AS pi
ON p.productid = pi.productidD
WHERE te.custid = '24'
GROUP BY pi.productiD;
However, this is not the problem in your query, the problem is the fact that you are abusing the functionality within MySql whereby you can select items that are not part of the group by:
Here you are telling the query to select 1 row per distinct value for pi.ProductID
, there could be multiple different values for o.Date1
and o.Date2
for each pi.ProductID
, and you are giving the query no hints as to which of these multiple possibilities to choose from.
Using columns in the select that are not in the group by is permitted by ANSI standards but only when the columns in the select are functionally dependant on columns within the group by, this is very hard to adhere to so most DBMS simply do not allow columns in the select that are not in the group by.
To demonstrate consider this dataset (T)
OrderID ProductID Date1 Date2
1 1 20130401 20130402
2 1 20130402 20130403
3 2 20130403 20130404
4 2 20130404 20130405
5 2 20130405 20130406
If (as in your case) you did:
SELECT OrderID, ProductID, Date1, Date2
FROM T
GROUP BY ProductID
You would probably get:
OrderID ProductID Date1 Date2
1 1 20130401 20130402
3 2 20130403 20130404
Just taking the first row for each product ID, however since the query is non deterministic it would be equally correct for the query to return the last row for each productID:
OrderID ProductID Date1 Date2
2 1 20130402 20130403
5 2 20130405 20130406
It is a common myth that adding order by will help this, e.g.
SELECT OrderID, ProductID, Date1, Date2
FROM T
GROUP BY ProductID
ORDER BY OrderID;
MySql determines the rows to be returned before applying the order by, so even adding ORDER BY OrderID
does not guarantee the first order for each productID is returned.
The MySQL Docs state
The server is free to choose any value from each group, so unless they are the same, the values chosen are indeterminate. Furthermore, the selection of values from each group cannot be influenced by adding an ORDER BY clause.
Finally to elaborate on what I said about functionally dependant columns, because in this dataset OrderID
is a primary key, according to ANSI standards you could write:
SELECT OrderID, ProductID, Date1, Date2
FROM T
GROUP BY OrderID;
So, while I have not really answered your question I hopefully have shown where you are going wrong. You really need to establish what you are trying to achieve by the group by, and if you still haven't got a solution then i'd suggest a new, more constructive question, with sample data and an expected output with clear rules as to why the expexted output is expexted.