Posted By

stavelin on 09/21/08


Tagged

select query Subquery NOTIN


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

umang_nine


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.