在这些连接表中加入2个其他表和2个条件

时间:2013-09-27 13:35:22

标签: php mysql join inner-join where

$result = mysql_query("SELECT DISTINCT subscriberMaster.id,    
subscriberMaster.locationCodeID, subscriberMaster.firstName,
subscriberMaster.lastName, subscriberMaster.company, subscriberMaster.address,
subscriberMaster.address2, subscriberMaster.city, subscriberMaster.province,
subscriberMaster.postalCode, subscriberMaster.country,
subscriberMaster.numLabels
FROM subscriberMaster
JOIN commodityMaster ON commodityMaster.subscriberID = subscriberMaster.id AND
commodityMaster.commodityID IN (" . $commodityquery2 . ")
JOIN industryMaster ON industryMaster.subscriberID = subscriberMaster.id AND
industryMaster.industryID IN (" . $industryquery2 . ")
WHERE subscriberMaster.locationCodeID = $locationCodeID AND
subscriberMaster.province IN (" . $provincequery2 . ") AND
(subscriberMaster.deleted = 0) AND (subscriberMaster.expiryDate = '00-00-00')
OR (subscriberMaster.expiryDate > NOW()) ORDER BY $orderBy ASC");   

此查询中的所有内容都正常工作,直到我添加了第二个连接,其中它也匹配主表中的相同ID。 Where子句一切正常,第一次加入两个ON条件都有效。这似乎是第二个JOIN子句的问题。

基本上,subscriberMaster是包含所有记录详细信息(地址,电话号码等)的主表。 CommodityMaster和IndustryMaster存储有关他们所从事的行业以及他们销售的商品的详细信息。

编辑** 这是我回应查询时出现的内容。

SELECT DISTINCT subscriberMaster.id, subscriberMaster.locationCodeID,
subscriberMaster.firstName, subscriberMaster.lastName, subscriberMaster.company,
subscriberMaster.address, subscriberMaster.address2, subscriberMaster.city,
subscriberMaster.province, subscriberMaster.postalCode, subscriberMaster.country,
subscriberMaster.numLabels FROM subscriberMaster
JOIN commodityMaster ON commodityMaster.subscriberID = subscriberMaster.id
JOIN industryMaster ON industryMaster.subscriberID = subscriberMaster.id
WHERE subscriberMaster.locationCodeID = 1 AND commodityMaster.commodityID IN ('1',
'2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19',
'20','21','22','23','24','25','26','27','28','29','30','31','32','33') AND
industryMaster.industryID IN ('1','3','2') subscriberMaster.province IN
('Ontario') AND (subscriberMaster.deleted = 0) AND (subscriberMaster.expiryDate =
 '00-00-00') OR (subscriberMaster.expiryDate > NOW())
ORDER BY company ASC

2 个答案:

答案 0 :(得分:0)

我会将您的附加约束从JOIN部分移动到WHERE部分。这是应该的地方。

SELECT DISTINCT 
    subscriberMaster.id,    
    subscriberMaster.locationCodeID, 
    subscriberMaster.firstName,
    subscriberMaster.lastName, 
    subscriberMaster.company, 
    subscriberMaster.address,
    subscriberMaster.address2, 
    subscriberMaster.city, 
    subscriberMaster.province,
    subscriberMaster.postalCode, 
    subscriberMaster.country,
    subscriberMaster.numLabels
    FROM subscriberMaster
        JOIN commodityMaster 
            ON commodityMaster.subscriberID = subscriberMaster.id 
        JOIN industryMaster 
            ON industryMaster.subscriberID = subscriberMaster.id 
    WHERE subscriberMaster.locationCodeID = $locationCodeID 
        AND commodityMaster.commodityID IN (" . $commodityquery2 . ")
        AND industryMaster.industryID IN (" . $industryquery2 . ")
        AND subscriberMaster.province IN (" . $provincequery2 . ") 
        AND (subscriberMaster.deleted = 0) 
        AND (subscriberMaster.expiryDate = '00-00-00')
        OR (subscriberMaster.expiryDate > NOW()) 
    ORDER BY $orderBy ASC

答案 1 :(得分:0)

将您的industryMaster JOIN更改为LEFT OUTER JOIN并查看它是否返回数据,我假设您没有subscribermaster中所有industrymaster个ID的行,请尝试此

SELECT DISTINCT subscriberMaster.id,    
subscriberMaster.locationCodeID, subscriberMaster.firstName,
subscriberMaster.lastName, subscriberMaster.company, subscriberMaster.address,
subscriberMaster.address2, subscriberMaster.city, subscriberMaster.province,
subscriberMaster.postalCode, subscriberMaster.country,
subscriberMaster.numLabels

FROM subscriberMaster
LEFT OUTER JOIN commodityMaster 
   ON commodityMaster.subscriberID = subscriberMaster.id 
   AND commodityMaster.commodityID IN (" . $commodityquery2 . ")

LEFT OUTER JOIN industryMaster 
   ON industryMaster.subscriberID = subscriberMaster.id 
   AND industryMaster.industryID IN (" . $industryquery2 . ")

WHERE subscriberMaster.locationCodeID = $locationCodeID AND
subscriberMaster.province IN (" . $provincequery2 . ") AND
(subscriberMaster.deleted = 0) AND (subscriberMaster.expiryDate = '00-00-00')
OR (subscriberMaster.expiryDate > NOW()) 
ORDER BY $orderBy ASC