Results 1 to 5 of 5

Thread: Flagging in one query what is returned by another

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    589

    Flagging in one query what is returned by another

    I have two tables.

    tblCategories - which holds a list of categories. Fields are CategoryID, CategoryDescription

    tblJobCategories which holds a list of categories selected for a specific job. Fields are JobCategoryID(Primary key), CategoryID and JobID

    When a user is looking at a new job they can select a categories link which displays a list of categories from tblCategories. They can select one or more of the categories and these are written to tblJobCategories.

    When they are editing the job, I want them to click on the Categories link to again display the list of Categories from tblCategories but, I'd like to show the checkboxes next to Categories already selected as being ticked.

    So, I have

    Code:
    Select CategoryID, CategoryDescription from tblCategories
    to get the generic list of categories and

    Code:
    Select CategoryID from tblJobCategories WHERE JobID = @JobID
    to get the list of Categories for that job.

    How can I 'combine' the two statements so I can flag which categories in the generic list already appear in the specific list?

    Thanks for any help.

  2. #2
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: Flagging in one query what is returned by another

    I can't completely understand what you are trying to accomplish, but if you are trying to limit records shown in your first query with the constraint in the second query, you could try:

    Code:
    "SELECT CategoryID, CategoryDescription " & _
    "FROM tblCategories INNER JOIN tblJobCategories ON tblCategories.CategoryID = tblJobCategores.CategoryID " & _
    "WHERE tblJobCategories.JobID = @JobID;"

  3. #3
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Flagging in one query what is returned by another

    Code:
    SELECT CategoryID, CategoryDescription, JobId
    FROM tblCategories 
    Left Join tblJobCategories ON tblCategories.CategoryID = tblJobCategories.CategoryID
                 And tblJobCategories.JobID = @JobID
    All records from tblCategories will be returned. The JobId field will contain Null if there is no match in tblJobCategories otherwise it will equal @JobId.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2006
    Posts
    589

    Re: Flagging in one query what is returned by another

    Thanks very much for that.

  5. #5
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: Flagging in one query what is returned by another

    Is that what he wanted? I had no idea.. Nice use of left join

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width