MySQL distinct不会返回Distinct Values

时间:2014-01-02 10:08:37

标签: mysql group-by distinct

我有一个奇怪的MySQL问题。出于某种原因,我的查询没有返回不同的值。正如它应该。

SELECT DISTINCT(market_devices.deviceId), 
               market_devices.id, 
               registerId, 
               deviceOs 
FROM   market_devices_link 
       INNER JOIN market_devices 
               ON market_devices_link.deviceId = market_devices.id 
WHERE  market_devices_link.shopid = '46' 
       AND ( market_devices.deviceId IS NOT NULL 
             AND market_devices.registerId IS NOT NULL)
       AND market_devices.registerId NOT LIKE 'NO_NOTIFICATION%';

使用此查询,我得到了9次结果,其中3次是deviceId ='356150053892215';

将查询重写为GROUP BY market_devices.DeviceId将导致7个值,而不会出现重复的deviceIds。 (见下面的查询)

SELECT market_devices.id, 
               registerId, 
               deviceos 
FROM   market_devices_link 
       INNER JOIN market_devices 
               ON market_devices_link.deviceId = market_devices.id 
WHERE  market_devices_link.shopid = '46' 
       AND ( market_devices.deviceId IS NOT NULL 
             AND market_devices.registerId IS NOT NULL)
       AND market_devices.registerId NOT LIKE 'NO_NOTIFICATION%'
GROUP BY market_devices.deviceId;

我们也遇到了性能问题,那么什么是更好的方法呢?请记住,每个不同的DeviceId只需要1个项目。 DISTINCT或GROUP BY会更好吗?

0 个答案:

没有答案