Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using IF statement in a SELECT WHERE

Status
Not open for further replies.

gmckayau

Programmer
May 11, 2010
2
AU
I have a table of updates that I wish to compare against the a database to find those records with differences.
Each update record has a cat_id.
Some updates have an alt_cat_id.
I need a select to search to search if these differ, but only search for the alt-cat_id if it is populated. I don't have a solution for this but thought the IF statement might help.

SELECT upd.key, upd.cat_id, upd.alt_cat_id, db.cat_id FROM db
JOIN update ON (upd.key = db.key)
WHERE upd.cat_id <> db.cat_id
OR upd.alt_cat_id NOT IN (SELECT cat_id FROM cats WHERE cats.key = upd.key);

The OR only needs to be executed if alt_cat_id is non-blank....
 
SELECT upd.key, upd.cat_id, upd.alt_cat_id, db.cat_id FROM db
JOIN update ON (upd.key = db.key)
WHERE upd.cat_id <> db.cat_id
OR
(upd.alt_cat_id is not null and upd.alt_cat_id NOT IN (SELECT cat_id FROM cats WHERE cats.key = upd.key))

Ian

 
Thank you. Now that I look at it, it seems obvious...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top