Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2004
    Posts
    1

    Exclamation Unanswered: Need help with sql query

    I am trying to run a simple sql query but must be doing something wrong. Basically there are two tables, one containing category information and another containing categories that should be blocked. I want to make a sql query that grabs all of the categories except those listed in the blocked list. Below is the sql string I used, instead of just the unblocked categories being returned it returns the rows of the unblocked categories and then another copy of all categories.

    SQL String I am using:
    "SELECT categories.catID, categories.catName FROM categories, blockedcategories WHERE categories.catID != blockedcategories.catID"

    Data in categories table

    catID | catName
    ----------------------
    1 | Test Category
    ----------------------
    2 | Test Category2
    ----------------------
    3 | Test Category3
    ----------------------
    4 | Test Category4


    Data in blockedcategories table

    catID
    ----------
    1
    ----------
    3


    Data returned when I run my sql string


    catID | catName
    ----------------------
    2 | Test Category2
    ----------------------
    4 | Test Category4
    ----------------------
    1 | Test Category
    ----------------------
    2 | Test Category2
    ----------------------
    3 | Test Category3
    ----------------------
    4 | Test Category4


    Desired results if SQL string worked correctly

    catID | catName
    ----------------------
    2 | Test Category2
    ----------------------
    4 | Test Category4

  2. #2
    Join Date
    Jun 2004
    Posts
    7
    try:

    SELECT DISTINCT categories.catID, categories.catName FROM categories WHERE categories.catID <> ALL (SELECT catId FROM blockedcategories);

    or maybe with WHERE NOT EXIST

    hope this helps, I'm tired

  3. #3
    Join Date
    Jun 2004
    Posts
    2

    Thumbs up Try it!

    Hi, Please try it..

    select categories.catID, categories.catName
    from categories where categories.catID not in(
    select blockedcategories.catID from blockedcategories)
    Last edited by ebin; 06-19-04 at 01:35. Reason: Spelling wrong

Posting Permissions

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