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 > Interesting query - hard to title

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-06-06, 10:33
Triune Triune is offline
Registered User
 
Join Date: Nov 2006
Posts: 14
Interesting query - hard to title

I have an 'albums' table and I have a 'restrictions' table. I want to pull all of the albums except the ones that are restricted from a certain territory.

e.g.
SELECT * FROM albums [except WHERE album.cat_no = restrictions.cat_no AND territory = 'GB']

How would I go about turning the bit in the brackets into a real life query? Bearing in mind some albums will have no restrictions.
Reply With Quote
  #2 (permalink)  
Old 11-07-06, 01:23
GongXi GongXi is offline
Registered User
 
Join Date: Jun 2004
Posts: 57
try this

SELECT * FROM albums WHERE album.cat_no not in (select cat_no from restrictions ) AND territory = 'GB'
Reply With Quote
  #3 (permalink)  
Old 11-07-06, 03:12
Kris Zywczyk Kris Zywczyk is offline
Registered User
 
Join Date: Nov 2006
Location: UK
Posts: 46
I would use something like that:

Code:
select a.* 
from albums a left outer join restrictrions r on a.cat_no = r.cat_no and
                                                 territory = 'GB'
where r.cat_no is null

Regards,
Kris
Reply With Quote
  #4 (permalink)  
Old 11-07-06, 03:19
Kris Zywczyk Kris Zywczyk is offline
Registered User
 
Join Date: Nov 2006
Location: UK
Posts: 46
I am sorry - I just entered onto improper forum

Abowe syntax is for MS SQL Server.
I am not sure if the syntax is the same on MySQL - the idea still should work.

Regards,
Kris
Reply With Quote
  #5 (permalink)  
Old 11-07-06, 04:48
Triune Triune is offline
Registered User
 
Join Date: Nov 2006
Posts: 14
Quote:
Originally Posted by GongXi
try this

SELECT * FROM albums WHERE album.cat_no not in (select cat_no from restrictions ) AND territory = 'GB'
That's almost it, except the territory column is in the restrictions table (which is in the sub query so not available). Any idea how to fix that?

Thanks, GongXi.
Reply With Quote
  #6 (permalink)  
Old 11-07-06, 07:31
Triune Triune is offline
Registered User
 
Join Date: Nov 2006
Posts: 14
Ack, sorry. I didn't have the restrictions table in the FROM clause.
Reply With Quote
  #7 (permalink)  
Old 11-07-06, 11:38
Triune Triune is offline
Registered User
 
Join Date: Nov 2006
Posts: 14
Okay, the query still doesn't do what I want it to. This is hard!

The query is looking for 'albums.cat_no' in 'territory.restrictions' - so none of the albums are found if no restrictions are set!

Any ideas anyone?
Reply With Quote
  #8 (permalink)  
Old 11-07-06, 11:47
Triune Triune is offline
Registered User
 
Join Date: Nov 2006
Posts: 14
Quote:
Originally Posted by Kris Zywczyk
I would use something like that:

Code:
select a.* 
from albums a left outer join restrictrions r on a.cat_no = r.cat_no and
                                                 territory = 'GB'
where r.cat_no is null

Regards,
Kris
Hi Kris,

I tried the following:
Quote:
SELECT DISTINCT(id), title, artist, cat_no, description, release_date FROM albums LEFT OUTER JOIN territory_restrictions ON albums.cat_no = territory_restrictions.restrictions_cat_no AND territory_restrictions.restrictions_territory = 'US' WHERE publish = 1 AND deleted != 1 AND territory_restrictions.restrictions_cat_no = NULL ORDER BY cat_no ASC LIMIT 0, 5
Which does not give me an error, but yields no results.
Reply With Quote
  #9 (permalink)  
Old 11-07-06, 21:54
GongXi GongXi is offline
Registered User
 
Join Date: Jun 2004
Posts: 57
you can try this,

SELECT * FROM albums WHERE album.cat_no not in (select cat_no from restrictions where territory = 'GB')
Reply With Quote
  #10 (permalink)  
Old 11-07-06, 22:32
GongXi GongXi is offline
Registered User
 
Join Date: Jun 2004
Posts: 57
Kris code should work, may be you have to check your data.
Reply With Quote
  #11 (permalink)  
Old 11-08-06, 04:20
Triune Triune is offline
Registered User
 
Join Date: Nov 2006
Posts: 14
Quote:
Originally Posted by GongXi
you can try this,

SELECT * FROM albums WHERE album.cat_no not in (select cat_no from restrictions where territory = 'GB')
That's it! Yay!!!!

Thank you so much GongXI! You genuis.
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