If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Need help with sql query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-18-04, 23:38
shaunshull shaunshull is offline
Registered User
 
Join Date: Jun 2004
Posts: 1
Exclamation 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
Reply With Quote
  #2 (permalink)  
Old 06-19-04, 00:19
kukusz kukusz is offline
Registered User
 
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
Reply With Quote
  #3 (permalink)  
Old 06-19-04, 00:33
ebin ebin is offline
Registered User
 
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 00:35. Reason: Spelling wrong
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On