MySQL返回行数为零

时间:2012-02-17 15:00:19

标签: mysql

表格布局

reject_data

+-----------+-----------------+------------------+---------------+
| reject_id | reject_location | reject_equipment | reject_time   |
+-----------+-----------------+------------------+---------------+
| 1         | 7               | 6                | 1326795921000 |
+-----------+-----------------+------------------+---------------+
| 2         | 7               | 1                | 1326796641000 |
+-----------+-----------------+------------------+---------------+
| 3         | 7               | 6                | 1326799521000 |
+-----------+-----------------+------------------+---------------+
| 4         | 6               | 5                | 1326800781000 |
+-----------+-----------------+------------------+---------------+
| 5         | 7               | 3                | 1326802281000 |
+-----------+-----------------+------------------+---------------+
| 6         | 7               | 4                | 1326802941000 |
+-----------+-----------------+------------------+---------------+
| 7         | 7               | 1                | 1326814161000 |
+-----------+-----------------+------------------+---------------+
| 8         | 6               | 2                | 1328026700000 |
+-----------+-----------------+------------------+---------------+

设备

+--------------+------------------+
| equipment_id | equipment_string |
+--------------+------------------+
| 1            | Microdoser       |
+--------------+------------------+
| 2            | Monoblock        |
+--------------+------------------+
| 3            | Valve Magnet     |
+--------------+------------------+
| 4            | Checkweigher     |
+--------------+------------------+
| 5            | Microleak        |
+--------------+------------------+
| 6            | Capper           |
+--------------+------------------+

查询

SELECT equipment_string AS eqpt, COUNT(reject_id) AS cnt
FROM reject_data
RIGHT OUTER JOIN equipment ON (reject_equipment = equipment_id) 
WHERE reject_location IN (1,2,7) AND equipment_id IN (1,2,3,4,5,6) AND reject_time BETWEEN 1329479810000 AND 1329483410000
GROUP BY equipment_id
ORDER BY cnt DESC

问题

如何更改我的查询以便它还返回零计数的设备?真的卡住了:(

感谢您的帮助。

注意:动态变量正常填充IN。

2 个答案:

答案 0 :(得分:2)

SELECT 
    e.equipment_string AS eqpt, 
    COUNT(reject_id) AS cnt

FROM equipment AS e

LEFT JOIN reject_data AS rd
    ON rd.reject_equipment = e.equipment_id
    AND rd.reject_location IN (1,2,7)
    AND rd.reject_time BETWEEN 1329479810000 AND 1329483410000

WHERE e.equipment_id IN (1,2,3,4,5,6)

GROUP BY e.equipment_id
ORDER BY cnt DESC

通过限制连接标准,可以减少MySQL需要检索的行数

答案 1 :(得分:1)

WHERE子句在<{em> JOIN之后进行过滤,因此会过滤掉JOIN失败的所有记录(自reject_location IN (1,2,7)起对那些人来说不是真的。您需要将这些限制移到ON子句:

SELECT equipment_string AS eqpt, COUNT(reject_id) AS cnt
FROM reject_data
RIGHT OUTER JOIN equipment
       ON reject_equipment = equipment_id
      AND reject_location IN (1,2,7)
      AND reject_time BETWEEN 1329479810000 AND 1329483410000
WHERE equipment_id IN (1,2,3,4,5,6)
GROUP BY equipment_id
ORDER BY cnt DESC

在这种情况下,您可能会发现更清楚的另一种方法是使用子查询而不是连接:

SELECT equipment_string AS eqpt,
       ( SELECT COUNT(1)
           FROM reject_data
          WHERE reject_equipment = equipment_id
            AND reject_location IN (1,2,7)
            AND reject_time BETWEEN 1329479810000 AND 1329483410000
       ) AS cnt
  FROM equipment
 WHERE equipment_id IN (1,2,3,4,5,6)
 ORDER BY cnt DESC