具有内部联接的MYSQL查询结果

时间:2015-03-02 09:03:28

标签: php mysql sql database

以下查询正在运行,但它多次给我相同的数据.... 建议我解决这个问题

"SELECT 
     cp.projectID,cp.projectName,cp.clientID,
     pv.vendorID,
     cv.vendorName,
     c.clientName,
     vp.productName,
     pp.sku
FROM clientProjects AS cp 
    INNER JOIN clients AS c ON c.clientID = cp.clientID
    INNER JOIN projectVendors AS pv ON cp.projectID = pv.projectID
    INNER JOIN clientVendors AS cv ON cv.vendorID = pv.vendorID
    INNER JOIN projectProducts AS pp ON cp.projectID = pp.projectID 
    INNER JOIN projectMaintenance AS pm ON cp.projectID = pm.projectID
    INNER JOIN vendorProducts AS vp ON pp.sku = vp.sku
    INNER JOIN projectFiles AS pf ON cp.projectID = pf.projectID
WHERE cp.projectName LIKE '".$projectName."' OR c.clientName LIKE '".$projectName."' OR pp.sku LIKE '".$projectName."' OR pm.sku LIKE '".$projectName."' "

1 个答案:

答案 0 :(得分:0)

尝试DISTINCT:

SELECT DISTINCT cp.projectID,
                cp.projectName,
                cp.clientID, 
                pv.vendorID, 
                cv.vendorName, 
                c.clientName, 
                vp.productName, 
                pp.sku
FROM clientProjects AS cp 
INNER JOIN clients AS c ON c.clientID = cp.clientID
INNER JOIN projectVendors AS pv ON cp.projectID = pv.projectID
INNER JOIN clientVendors AS cv ON cv.vendorID = pv.vendorID
INNER JOIN projectProducts AS pp ON cp.projectID = pp.projectID 
INNER JOIN projectMaintenance AS pm ON cp.projectID = pm.projectID
INNER JOIN vendorProducts AS vp ON pp.sku = vp.sku
INNER JOIN projectFiles AS pf ON cp.projectID = pf.projectID
WHERE cp.projectName LIKE '".$projectName."' OR 
      c.clientName LIKE '".$projectName."' OR 
      pp.sku LIKE '".$projectName."' OR 
      pm.sku LIKE '".$projectName."' "