0

我想加入两个具有公共列 stationcode(TABLE vfdefects & tempsubstation)的表。问题是 tempsubstation 具有重复的列值,而我只想要每个唯一值的计数。在我目前的尝试中,使用LEFT JOINGROUP BY

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
4

2 回答 2

1

改变:

FROM tempsubstation ts

至:

FROM (SELECT * FROM tempsubstation
      GROUP BY columnWithDuplicateValues) ts
于 2013-10-25T10:55:17.400 回答
-1

你是如何运行查询的?如果通过 PHP 则简化 SQL 并在 PHP 读取循环中运行第二个 SQL。

于 2013-10-25T10:55:10.600 回答