我一直试图让这个查询起作用,但我现在很蠢。 问题在于JOIN与GROUP_CONCAT。我无法想象如何让它发挥作用。
我想要的是从RFQ获取数据以及加入客户等等,一切都很好,直到我不得不更改供应商字段,以便每个RFQ行可以有多个供应商。我创建了新表RFQsSuppliers,其中我将Supplier.ID和RFQs.ID组合在一起,我有表供应商,其中包含名称和其他内容。
我希望让供应商将所有供应商与','分开。
我的查询:
$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());
目前,错误是:'字段列表'中的未知列'Supplier.Name'
以下是我的表设计的一些示例:
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
我试图只通过以下查询获得供应商:
$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());
但每行上的print_r仅返回以下内容:
数组([供应商] => Sup1,Sup1)数组([供应商] =>)数组([供应商] => Sup4,Sup4)数组([供应商] =>)数组([供应商] =>)数组([供应商] =>)
有什么想法吗?
答案 0 :(得分:2)
JOIN (
SELECT RFQs.ID,
GROUP_CONCAT(Supplier**s**.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
试试
我相信您错过了GROUP_CONCAT表名中的's',因为您将其作为Supplier.Name而不是根据表格的Suppliers.name
修改强>
此外,您指的是WHERE和ORDER BY子句中的RFQs.LastEdited,但是您将表RFQ别名为Pullero,因此需要更改为Pullero.LastEdited
修改编辑重新格式化以使用JOIN而不是SUBQUERY
SELECT
Pullero.DateAdded as DateAdded,
Customers.Name as customer,
Pullero.ID as RFQID,
GROUP_CONCAT(Suppliers.Name) AS Suppliers,
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 AS Pullero
LEFT JOIN RFQsSuppliers ON RFQsSuppliers.RFQID = Pullero.ID
LEFT JOIN Suppliers ON RFQSuppliers.SupplierID = Suppliers.ID
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 Pullero.LastEdited > '$lastedited'
ORDER BY Pullero.LastEdited ASC
或者对于使用原始子查询的示例,RFQs链接似乎非常丰富,所以我调整了它+已将“供应商”列表添加到输出中
$result = mysql_query("SELECT Pullero.DateAdded as DateAdded,
RFQsSuppliers.Suppliers,
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 RFQsSuppliers.RFQID,
GROUP_CONCAT(Suppliers.Name) AS Suppliers
FROM RFQsSuppliers.RFQID
JOIN Suppliers ON RFQsSuppliers.SupplierID = Suppliers.ID
GROUP BY RFQsSuppliers.RFQID
) AS 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 Pullero.LastEdited > '$lastedited'
ORDER BY Pullero.LastEdited ASC
") or die(mysql_error());
编辑更新了第二部分问题的查询
$result = mysql_query("SELECT RFQs.ID, GROUP_CONCAT(Suppliers.Name) as Suppliers
FROM RFQs
LEFT JOIN RFQsSuppliers ON RFQs.ID=RFQsSuppliers.RFQID
LEFT JOIN Suppliers ON RFQsSuppliers.SupplierID=Suppliers.ID
GROUP BY RFQs.ID
") or die(mysql_error());
答案 1 :(得分:0)
您引用了列Supplier.Name
,但表连接位于名为Suppliers
的表中,复数。
(SELECT RFQs.ID,
/* Should be plural Suppliers.Name here... */
GROUP_CONCAT(Suppliers.Name) AS Suppliers
FROM RFQs
LEFT JOIN RFQsSuppliers ON RFQs.ID = RFQsSuppliers.RFQID
JOIN Suppliers ON RFQsSuppliers.SupplierID = Suppliers.ID
GROUP BY RFQs.ID)
稍后,在您的最终WHERE
子句中,您引用了RFQs
,但是您将该表别名为Pullero
:
FROM RFQs Pullero
更改WHERE
子句以使用
WHERE Pullero.LastEdited > '$lastedited'
ORDER BY Pullero.LastEdited ASC