简体   繁体   English

MySQL-搜索NULL值

[英]MySQL - search for NULL values

I've tested a weird query that involves me searching for non-existent records. 我已经测试了一个奇怪的查询,其中涉及到我搜索不存在的记录。

In a nutshell, I have 3 tables : tbl_customers tbl_customers_notes tbl_customers_pipelines 简而言之,我有3个tablestbl_customers tbl_customers_notes tbl_customers_pipelines

Customers and Notes are pretty obvious, but pipelines aren't. 客户和Notes很明显,但管道却不明显。 Basically, pipelines are just what administrators are assigned to a single customerid . 基本上,管道就是管理员分配给单个customerid

Here's the table structure for the pipelines: 这是管道的表结构:

ID | ID | customerid | 客户ID | adminid | adminid |

I didn't have to create the ID column, but I did anyway, and it's auto-incremented. 我不必创建ID列,但是无论如何我都做了,它会自动递增。

Every customer record starts out with no joining pipeline records. 每个客户记录都是从没有加入管道记录开始的。 So it's empty when a customerid is first created. 因此,在首次创建customerid时为空。 If I want to assign administrators to manage that new customerid, and simply add a record for each adminid: 如果要分配管理员来管理该新的customerid,只需为每个adminid添加一条记录:

ID  | customerid | adminid
---------------------------
1   |    45      |   6
2   |    45      |   8
3   |    45      |   10

This means the customerid: 45 has 3 adminid's assigned. 这意味着customerid:45已分配了3个adminid。
(btw, adminid's are like sales ppl) (顺便说一句,adminid就像销售人员)

I have a query below that lets me search for customerid's that's missing notes from a specific adminid. 我下面有一个查询,使我可以搜索缺少特定adminid注释的customerid。 And it works great: 而且效果很好:

SELECT c.*
FROM `tbl_customers` c 
LEFT JOIN `tbl_customers_pipelines` cp ON c.customerid = cp.customerid 
WHERE c.customerid 
          NOT IN(SELECT n.customerid 
                 FROM `tbl_customers_notes` n 
                 WHERE n.datesubmitted BETWEEN '2011-12-01 14:00:00' 
                 AND '2011-12-03 13:59:59' 
                 AND n.categoryid IN(10)
                )

AND cp.adminid = 855 

You'll notice I'm performing a subquery after the WHERE clause. 您会注意到我在WHERE子句之后执行子查询。 And this does work as expected. 这确实按预期工作。 I'm displaying just the customer records that have specific notes, and have a specific adminid in the pipelines table. 我只显示具有特定注释并在管道表中具有特定adminid的客户记录。

I want to do the inverse of this. 我想做相反的事情。
And search for customer records that are missing pipeline/admin records entirely. 并搜索完全缺少管道/管理员记录的客户记录。 I'm essentially looking for customerid's that have notes, but no adminid attached. 我本质上是在寻找有注释但没有adminid的customerid。 And this is where I'm running into some confusion on building the query. 这就是我在构建查询时遇到的困惑的地方。

My only thought was to replace the "AND cp.adminid = 855" with "AND ISNULL(cp.ID)" 我唯一的想法是将“ AND cp.adminid = 855”替换为“ AND ISNULL(cp.ID)”

And that produces no results. 而且不会产生任何结果。 But there are indeed records. 但是确实有记录。 So the query is just off. 因此查询刚刚结束。 Anyone see something I'm missing? 有人看到我想念的东西吗?

Try: 尝试:

AND cp.ID IS NULL

instead of 代替

AND cp.adminid = 855

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM