MySQL GROUP BY列忽略特定值

时间:2016-10-13 18:52:24

标签: mysql

我对使用GROUP BY仅显示唯一来电者ID的电话呼叫表进行查询。问题是,如果呼叫者有呼叫者ID阻止他们的呼叫者ID显示为"未知"并且客户并不希望将所有未知数总结在一起。所以基本上,不仅仅是GROUP BY caller_id,我需要以某种方式执行GROUP BY caller_id IF caller_id != 'Unknown'

之类的操作

这甚至可能吗?如果可能的话,我想避免在PHP中进行所有的组处理。

1 个答案:

答案 0 :(得分:2)

您可以执行以下操作:

SELECT caller_id FROM phone_calls WHERE caller_id != 'Unknown' GROUP BY caller_id;

或考虑 DISTINCT - 在大多数情况下它更快 - 如果你在caller_id上创建索引,性能通常是相同的,但如果不是,DISTINCT会更好。如果您需要:

  

...只显示唯一的来电者ID ..

但也许对于你的情况(做聚合或类似事情)你不能使用它,但以防万一:

SELECT DISTINCT caller_id FROM phone_calls WHERE caller_id != 'Unknown';

- 在评论讨论后编辑

SELECT * FROM callers;                                                                                                                                                                                             
+----+-----------+-----------+                                                                                                                                                                                                        
| id | caller_id | call_time |                                                                                                                                                                                                        
+----+-----------+-----------+                                                                                                                                                                                                        
|  1 | abc       |        24 |                                                                                                                                                                                                        
|  2 | abc       |        16 |                                                                                                                                                                                                        
|  3 | xyz       |        10 |                                                                                                                                                                                                        
|  4 | xyz       |        10 |                                                                                                                                                                                                        
|  5 | Unknown   |        11 |                                                                                                                                                                                                        
|  6 | Unknown   |        12 |                                                                                                                                                                                                        
|  7 | Unknown   |        13 |                                                                                                                                                                                                        
|  8 | xyz       |         1 |                                                                                                                                                                                                        
|  9 | abc       |        10 |                                                                                                                                                                                                        
+----+-----------+-----------+   

SELECT caller_id, SUM(call_time) FROM callers 
WHERE caller_id != 'Unknown' 
GROUP BY caller_id;                                                                                                                     
+-----------+----------------+                                                                                                                                                                                                        
| caller_id | SUM(call_time) |                                                                                                                                                                                                        
+-----------+----------------+                                                                                                                                                                                                        
| abc       |             50 |                                                                                                                                                                                                        
| xyz       |             21 |                                                                                                                                                                                                        
+-----------+----------------+                                                                                                                                                                                                        

SELECT caller_id, SUM(call_time) FROM callers
GROUP BY caller_id;                                                                                                                                                  
+-----------+----------------+                                                                                                                                                                                                        
| caller_id | SUM(call_time) |                                                                                                                                                                                                        
+-----------+----------------+                                                                                                                                                                                                        
| abc       |             50 |                                                                                                                                                                                                        
| Unknown   |             36 |                                                                                                                                                                                                        
| xyz       |             21 |                                                                                                                                                                                                        
+-----------+----------------+                                                                                                                                                                                                        

SELECT caller_id, SUM(call_time) as total_time FROM callers 
WHERE caller_id != 'Unknown' 
GROUP BY caller_id 
UNION 
SELECT caller_id, call_time FROM callers 
WHERE caller_id = 'Unknown';                             
+-----------+------------+                                                                                                                                                                                                             
| caller_id | total_time |                                                                                                                                                                                                             
+-----------+------------+                                                                                                                                                                                                             
| abc       |         50 |                                                                                                                                                                                                             
| xyz       |         21 |                                                                                                                                                                                                             
| Unknown   |         11 |                                                                                                                                                                                                             
| Unknown   |         12 |                                                                                                                                                                                                             
| Unknown   |         13 |                                                                                                                                                                                                             
+-----------+------------+                                                                                                                                                                                                             

SELECT caller_id, SUM(call_time) as total_time FROM callers
GROUP BY caller_id, 
      (case when caller_id = 'Unknown' 
       AND id is not null 
       then id end
       );                                                                   
+-----------+------------+                                                                                                                                                                                                             
| caller_id | total_time |                                                                                                                                                                                                             
+-----------+------------+                                                                                                                                                                                                             
| abc       |         50 |                                                                                                                                                                                                             
| Unknown   |         11 |                                                                                                                                                                                                             
| Unknown   |         12 |                                                                                                                                                                                                             
| Unknown   |         13 |                                                                                                                                                                                                             
| xyz       |         21 |                                                                                                                                                                                                             
+-----------+------------+