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 > a simple sub query not working

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-08-05, 22:04
bobd303 bobd303 is offline
Registered User
 
Join Date: Dec 2003
Location: Gandhinagar India
Posts: 22
Unhappy a simple sub query not working

hi have 2 tables

a)category
+------------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+--------------+------+-----+---------+----------------+
| categoryId | int(11) | | PRI | NULL | auto_increment |
| categoryTitleEnglish | varchar(100) | YES | | NULL | |
| categoryTitleFrench | varchar(100) | YES | | NULL | |
| categoryDetailsEnglish | mediumtext | YES | | NULL | |
| categoryDetailsFrench | mediumtext | YES | | NULL | |
| superCategoryId | int(11) | YES | | NULL | |
| subCategoriesId | mediumtext | YES | | NULL | |
| imageURL | mediumtext | YES | | NULL | |
+------------------------+--------------+------+-----+---------+----------------

b)items table
+---------------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------------+--------------+------+-----+---------+----------------+
| itemId | int(11) | | PRI | NULL | auto_increment |
| modelNumber | varchar(20) | YES | | NULL | |
| itemNameEnglish | varchar(100) | YES | | NULL | |
| itemNameFrench | varchar(100) | YES | | NULL | |
| itemPriceCAD | varchar(10) | YES | | NULL | |
| itemDescriptionEnglish | mediumtext | YES | | NULL | |
| itemDescriptionFrench | mediumtext | YES | | NULL | |
| imageURL | mediumtext | YES | | NULL | |
| stocksAvailable | char(1) | YES | | NULL | |
| categoryId | int(11) | | | 0 | |
| imageAvailableFinishesURL | mediumtext | YES | | NULL | |
+---------------------------+--------------+------+-----+---------+----------------+

I WANT TO SELECT CATEGORIES FOR WHICH THERE are no ITEMS..

but the following query is not working.. please help

select categoryId from category where categoryId NOT IN (select categoryId from items)

I GET the following error
ERROR 1064: You have an error in your SQL syntax near 'select categoryId from items)' at line 1

thanks
__________________
Reply With Quote
  #2 (permalink)  
Old 02-08-05, 22:17
guelphdad guelphdad is offline
Registered User
 
Join Date: Mar 2004
Posts: 440
what version of mysql? Can't do subquery prior to 4.1
Reply With Quote
  #3 (permalink)  
Old 02-08-05, 22:32
bobd303 bobd303 is offline
Registered User
 
Join Date: Dec 2003
Location: Gandhinagar India
Posts: 22
mysql Ver 11.18 Distrib 3.23.54, for redhat-linux-gnu (i386)
so i will update my mysql
__________________
Reply With Quote
  #4 (permalink)  
Old 02-08-05, 23:14
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
categories for which there are no items --
Code:
select c.categoryId
     , c.categoryTitleEnglish
     , c.categoryTitleFrench
     , c.categoryDetailsEnglish
     , c.categoryDetailsFrench
     , c.imageURL 
  from category as c
left outer
  join items as i
    on c.categoryId 
     = i.categoryId 
 where i.categoryId is null
works prior to 4.1 too
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 02-09-05, 13:54
bobd303 bobd303 is offline
Registered User
 
Join Date: Dec 2003
Location: Gandhinagar India
Posts: 22
Thumbs up

thanks..that was perfect
__________________
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