2013-10-25 45 views
0

我想连接两个具有共同列stationcode(TABLE vfdefects & tempsubstation)的表。 问题是tempsubstation有重复的列值,而我只想要每个唯一值的计数。以我目前的尝试它使用LEFT JOINGROUP BYMYSQL:左连接两个表使用不同的值从一个表

TABLE A ID|stationID|CODE

TABLE B A|B|C|D|stationcode

加倍重复的条目值

查询如下:

SELECT DISTINCT  TS.substationid,'20130924',ts.employeeid,ts.substationcode,ts.manufacturingproductid,ts.assemblylineid,ts.teamid,ts.subofficecode, 
TA.AllowID,ta.OtherAllowanceID,ta.allowname,ta.minbenchmark,ta.maxbenchmark,COUNT(vfd.DefectsID) Achieved 

FROM tempsubstation ts 
LEFT JOIN TempAllowances ta ON ts.manufacturingproductid=ta.manufacturingproductid AND ts.subofficecode=ta.subofficecode AND ta.teamid=ts.teamid 
LEFT JOIN wms_assemblyqcmapping aqc ON ts.subofficeid=aqc.subofficeid AND ts.manufacturingproductid=aqc.manufacturingproductid AND ts.assemblylineid=aqc.assemblylineid 
LEFT JOIN wms_vfdefects vfd ON vfd.QCStationCode=aqc.QCStationCode AND ts.SubstationCode =vfd.MFGStationNum AND DATE(vfd.CreationDate)='2013-09-24' 
WHERE ta.AllowID=42 
GROUP BY ta.minbenchmark,ta.maxbenchmark,ts.substationid 
HAVING COUNT(vfd.DefectsID)>=ta.minbenchmark AND COUNT(vfd.DefectsID)<=ta.maxbenchmark 
+0

使用子查询,消除重复的加入 – Barmar

+0

我使用临时表,又在一个子查询,同时运行父查询过我不能引用它。有另一种方法吗? – Salik

回答

1

变化:

FROM tempsubstation ts 

到:

FROM (SELECT * FROM tempsubstation 
     GROUP BY columnWithDuplicateValues) ts 
-1

你怎么样运行查询?如果通过PHP,然后简化SQL并在PHP读取循环中运行第二个SQL。