反向和SQL查询

时间:2014-05-28 10:22:39

标签: sql

我有一个正常运行的SQL查询...

SELECT COUNT(*) AS count FROM g_lead_d a JOIN g_lead b ON b.id = a.l_id WHERE b.date_posted LIKE '%$2014-01-01%' AND a.form_id LIKE '654' AND a.field_number = '22'

这将返回带有field_number 22的所有值,我现在要做的是再次运行此查询但反转,以便显示所有没有field_number 22的值。

我应该怎样接近?


**更新**

我知道尝试使用此查询....

SELECT COUNT(*) AS count FROM g_lead_d a JOIN g_lead b ON b.id = a.l_id WHERE b.date_posted LIKE '%$2014-01-01%' AND a.form_id LIKE '654' AND a.field_number <> '22'

这给了我一个意想不到的结果,而不是返回预期的结果 10 ,它返回 27483

我的数据集很大,但简化的样本低于.....

g_lead_d
id  |    l_id  |  form_id  |  field_number  |  value
----------------------------------------------------
1   |     1    |    654    |      22        | value1
2   |     2    |    654    |      22        | value8        
3   |     3    |    654    |      53        | value3
4   |     4    |    654    |      67        | value4
5   |     5    |    654    |      22        | value8
6   |     6    |    654    |      26        | value2
7   |     7    |    654    |      22        | value9


g_lead
id  |  form_id  |  date_created
-------------------------------
1   |    654    |   2014-01-01        
2   |    654    |   2014-01-01        
3   |    654    |   2014-01-01        
4   |    654    |   2014-01-01        
5   |    654    |   2014-01-01
6   |    654    |   2014-01-01        
7   |    654    |   2014-01-01  

2 个答案:

答案 0 :(得分:2)

而不是使用

a.field_number = '22'

尝试

a.field_number <> '22'

因此field_number不等于'22'

答案 1 :(得分:1)

试试这个:

SELECT COUNT(*) AS count 
FROM g_lead_d a JOIN g_lead b ON b.id = a.l_id 
WHERE b.date_posted LIKE '%$2014-01-01%' AND a.form_id LIKE '654' 
AND a.field_number <> '22'