INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Simple Query With Conditional

Simple Query With Conditional

(OP)
The query below is a simple and functional one that is bringing results but it is not bringing results when lc2.BaseCategory is empty as it sometimes is. When it has a value, it must match lc.ID but when it's empty, I still need the other data. I used to be a wiz at this but I'm afraid I've gotten quite rusty so can someone please assist?

CODE

SELECT lc2.ID, lc.CategoryName AS BaseCategory, lc2.CategoryName AS SubCategory,
sp.PathName, lc2.SubFolder, lc2.FileName 
FROM lookup_categories lc, lookup_categories lc2, site_paths sp 
WHERE lc.SitePath = sp.ID 
AND lc2.BaseCategory = lc.ID 
GROUP BY lc.CategoryName, SubCategory
ORDER BY lc.CategoryName 

RE: Simple Query With Conditional

How about:

CODE

SELECT lc2.ID, lc.CategoryName AS BaseCategory, lc2.CategoryName AS SubCategory,
sp.PathName, lc2.SubFolder, lc2.FileName 
FROM lookup_categories lc, lookup_categories lc2, site_paths sp 
WHERE lc.SitePath = sp.ID 
AND (lc2.BaseCategory = lc.ID OR lc2.BaseCategory = '')
GROUP BY lc.CategoryName, SubCategory
ORDER BY lc.CategoryName 

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech

RE: Simple Query With Conditional

(OP)
Thank you for the suggestion. I had actually already tried several variations of that but they either bring up too many results (more times than the content of the two tables combined!) or the same result as my posted code. In other words, yours gives the same 21 rows as mine so I'll probably have to rethink the query itself. You may have noticed that it is trying to join to itself which is probably most of the problem.

For example, lookup_categories contains fields:

ID | CategoryName | BaseCategory | SitePath | SubFolder | FileName

with values like:

5 | Contacts | NULL | 11 | NULL | NULL

while a mating row might have:

24 | Forum | 5 | 3 | forum | NULL

In the mating row, the 5 refers to the ID from the Contacts value so the expected result of the query is:

24 | Contacts | Forum | /path/subfolder/ | forum | NULL

and the query is indeed giving it as it should but missing are entries like:

5 | Contacts | NULL | /path/ | NULL | NULL

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close