We Recommend

SQL Cookbook SQL Cookbook
Written in O'Reilly's popular Problem/Solution/Discussion style, the SQL Cookbook is sure to please. Anthony's credo is: "When it comes down to it, we all go to work, we all have bills to pay, and we all want to go home at a reasonable time and enjoy what's still available of our days." The SQL Cookbook moves quickly from problem to solution, saving you time each step of the way.


Posted By

stavelin on 09/21/08


Tagged

select query Subquery NOTIN


Versions (?)


Find all categories NOT used in current item


Published in: SQL 


It's easy to find the categories used on one item, but find all categories NOT used on current item? This snippet stores a "NOT IN" query.

This is used with PHP & mySQL. The query can be written differently on other databases, but this seems to work with mySQL.

  1. FUNCTION findunUsedCats($aid){
  2. $query = "SELECT cat_tbl.cid, cat_tbl.category FROM cat_tbl WHERE cat_tbl.category NOT IN (SELECT cat_tbl.category FROM cat_tbl, art_has_cat_tbl WHERE cat_tbl.cid = art_has_cat_tbl.cid AND aid =$aid);";
  3. $query = mysql_query($query)OR die("nope..".mysql_error());
  4. RETURN $query;
  5. }

Report this snippet 

You need to login to post a comment.