LEFT JOIN W / FILTER

时间:2017-01-24 15:44:36

标签: sql sql-server

我尝试使用LEFT JOIN来生成COUNT,但我在格式上苦苦挣扎,因为我正在使用WHERE过滤掉计数。我需要两个表上的WHERE。这就是我所拥有的

SELECT Count(rL.SERIAL_NUMBER)
FROM   [ROBOTICS_OPTICS_UAT].[dbo].[AOF_OPTIC_RESULTS] AS rL
WHERE  EXISTS (SELECT *
               FROM   [ROBOTICS_OPTICS_UAT].[dbo].[AOF_ORDER_OPTICS] AS oL
               WHERE  oL.[SERIAL_NUMBER] = rL.[SERIAL_NUMBER]
                      AND rL.[REJECT] = 0) 

我将AND oL.[SO_LINE_NUMBER] = '43776'添加到混音后,会产生0计数。表中存在该值,并且我试图多次重写这个值。我错过了什么或做错了什么?

2 个答案:

答案 0 :(得分:1)

SELECT  COUNT(rL.SERIAL_NUMBER)
FROM    [ROBOTICS_OPTICS_UAT].[dbo].[AOF_OPTIC_RESULTS] AS rL
INNER JOIN [ROBOTICS_OPTICS_UAT].[dbo].[AOF_ORDER_OPTICS] AS oL ON  oL.[SERIAL_NUMBER] = rL.[SERIAL_NUMBER]
WHERE
rL.[REJECT] = 0  AND oL.[SO_LINE_NUMBER] = '43775'

答案 1 :(得分:0)

你应该真正分开这些条件。 。 。一个用于子查询,另一个用于外部查询:

SELECT Count(rL.SERIAL_NUMBER)
FROM [ROBOTICS_OPTICS_UAT].[dbo].[AOF_OPTIC_RESULTS] rL
WHERE EXISTS (SELECT 1
              FROM  [ROBOTICS_OPTICS_UAT].[dbo].[AOF_ORDER_OPTICS] oL
              WHERE oL.[SERIAL_NUMBER] = rL.[SERIAL_NUMBER] AND
                    oL.[SO_LINE_NUMBER] = '43776'
             ) AND
       rL.[REJECT] = 0;

但是,我不认为这会导致您的特定问题。