Am I using this "IF" function right?

If not, please correct. It looks good to me…

SELECT b.id AS id, 
        b.category_type AS category_type, 
        IF(b.category_type = 0, b.name, c.name) AS name ";
FROM `#__budget_categories` AS b, `#__k2_categories` AS c ";
WHERE (b.user_id='".$userID."') ";
AND (b.category_type = c.id); ";

Basically, the category type can either have a value of zero, then the regular budget_category’s name field will do. Or, if the category type is not zero, then the code should instead look in the k2_categories for the name.

Understand?