views:

28

answers:

1

How to use grouping correctly in this query:

$sQuery = "SELECT id,DATE(A.Inspection_datetime) AS Date,
                              A.Model, COUNT(A.Serial_number) AS Qty,
                              B.name
                      FROM inspection_report AS A
                      LEFT JOIN Employee AS B
                      ON A.NIK=B.NIK
                      GROUP BY Date, B.name".$sWhere.$sOrder.$sLimit;

i'm really new in concatenation.

A: 

You need to GROUP BY both 'id' and 'A.Model' too, at least in most SQL DBMS (MySQL has laxer rules in this area). You need GROUP BY to come after the WHERE clause and before the ORDER BY clause. Hence:

$sGroupBy = " GROUP BY id, Date, A.Model, B.Name ";
$sQuery = "SELECT id,DATE(A.Inspection_datetime) AS Date,
                          A.Model, COUNT(A.Serial_number) AS Qty,
                          B.name
                  FROM inspection_report AS A
                  LEFT JOIN Employee AS B
                  ON A.NIK=B.NIK " .
                  $sWhere.$sGroupBy.$sOrder.$sLimit;

Note that you must make sure there are spaces to separate the various clauses that you concatenate together; I added a space at each end of $sGroupBy (and after B.NIK) to make sure that part was OK.

Jonathan Leffler
what is the function of space on concat? are you have tutorial's site for concat? i want learn more.
klox
The key point about the spaces is to ensure you don't have `A.NIK=B.NIKWHERE B.EmpID = 13GROUP BY id, ... B.NameORDER BY DateLIMIT 10` - where the keywords are running into the previous token, leading to syntax errors. I don't have a particular online reference to point you at - sorry.
Jonathan Leffler