加入两个额外的表,两个ON条件

时间:2013-09-27 16:59:30

标签: php mysql arrays join conditional-statements

$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

0 个答案:

没有答案