I have been trying to get this query to work but I am stumbed right now. Problem is on the JOIN with GROUP_CONCAT in it. I just can't figure how to get it working.
What I want is to get buntch of data from RFQs and joining Customers, etc. to it and everything was fine until I had to change Suppliers field so that it could have multiple suplliers per one RFQs row. I created new tables RFQsSuppliers where I combine Supplier.ID's and RFQs.ID's and I have table Suppliers that contains the names and other stuff.
I want to get Suppliers to have all suppliers separated with ','.
My Query:
$result = mysql_query("SELECT Pullero.DateAdded as DateAdded,
Customers.Name as customer,
Pullero.ID as RFQID,
Ships.Name as ship,
Pullero.CustomerRef as CustomerRef,
Contacts.FirstName as contactF,
Contacts.LastName as contactL,
Contacts.Email as contactE,
Users.tunnus as handler,
RFQStatus.Name as status,
Pullero.Description as RFQDescription,
Pullero.LastEdited as LastEdit
FROM RFQs Pullero
JOIN (SELECT RFQs.ID,
GROUP_CONCAT(Supplier.Name) AS Suppliers
FROM RFQs
LEFT JOIN RFQsSuppliers ON RFQs.ID = RFQsSuppliers.RFQID
JOIN Suppliers ON RFQsSuppliers.SupplierID = Suppliers.ID
GROUP BY RFQs.ID)
RFQsSuppliers ON Pullero.ID = RFQsSuppliers.RFQID
LEFT JOIN Ships ON RFQ.ShipID=Ships.ID
LEFT JOIN Contacts ON RFQ.ContactID=Contacts.ID
LEFT JOIN Customers ON RFQ.CustomerID=Customers.idCustomers
LEFT JOIN Users ON RFQ.PriJobHandler=Users.id
LEFT JOIN RFQStatus ON RFQ.StatusID=RFQStatus.ID
WHERE RFQs.LastEdited > '$lastedited'
ORDER BY RFQs.LastEdited ASC
") or die(mysql_error());
At the moment, error is :Unknown column 'Supplier.Name' in 'field list'
EDIT
Below is some expample of my Table desing:
RFQs
ID | DateAdded | CustomerID | ShipID | LastEdited | StatusID ...
/* -------------------------------------- */
Suppliers
ID | Name | CountryID
1 Sup1 2
2 Sup2 5
3 Sup3 3
4 Sup4 3
/* -------------------------------------- */
RFQsSuppliers
ID | RFQID | SupplierID
1 1 4
2 2 3
3 56 3
4 4 3
5 39 1
6 56 1
7 4 4
I tried to get only the suppliers with following query:
$result = mysql_query("SELECT Suppliers.Name as Suppliers
FROM RFQs
LEFT JOIN RFQsSuppliers ON RFQs.ID=RFQsSuppliers.SupplierID
LEFT JOIN Suppliers ON RFQsSuppliers.SupplierID=Suppliers.ID
GROUP BY RFQs.ID
") or die(mysql_error());
But print_r on each row returns only following:
Array ( [Suppliers] => Sup1,Sup1 ) Array ( [Suppliers] => ) Array ( [Suppliers] => Sup4,Sup4 ) Array ( [Suppliers] => ) Array ( [Suppliers] => ) Array ( [Suppliers] => )
Any ideas?