coffeemonitor
coffeemonitor

Reputation: 13120

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

Customers and Notes are pretty obvious, but pipelines aren't. Basically, pipelines are just what administrators are assigned to a single customerid.

Here's the table structure for the pipelines:

ID | customerid | adminid |

I didn't have to create the ID column, but I did anyway, and it's auto-incremented.

Every customer record starts out with no joining pipeline records. So it's empty when a customerid is first created. If I want to assign administrators to manage that new customerid, and simply add a record for each adminid:

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

This means the customerid: 45 has 3 adminid's assigned.
(btw, adminid's are like sales ppl)

I have a query below that lets me search for customerid's that's missing notes from a specific adminid. 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. 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.

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. 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 that produces no results. But there are indeed records. So the query is just off. Anyone see something I'm missing?

Upvotes: 0

Views: 2298

Answers (1)

xbrady
xbrady

Reputation: 1703

Try:

AND cp.ID IS NULL

instead of

AND cp.adminid = 855

Upvotes: 5

Related Questions