Buy here

  • Buy music products with drumwright
  • Buy music products with Andertons Music Company
  • Buy music products with Scan Computers
  • Buy music products with Red Dog Music
  • Buy music products with Hartnolls Guitars
  • Buy music products with Thomann
  • Buy music products with Professional Music Technology
Old 03-25-2009, 10:30 AM   #1
dafuzz
Senior Member
 
dafuzz's Avatar
 
Join Date: Feb 2008
Location: SE London
Posts: 3,079
Default SQL / Relational Db Question. Nude content!

Sorry not nude, I meant nerd

I have a FileTable and a KeywordTable, and a FileKeywordTable to resolve the many-to-many relationship. The FileTable holds info on the FileID, Filename and Filetype (e.g text, powertab). The KeywordTable is just Keyword and KeywordID. The linker table is just FileID and KeywordID.

The first part of the SQL query goes:

Code:
SELECT DISTINCT FileTable.FileName 
FROM (FileKeywordTable INNER JOIN FileTable ON FileKeywordTable.FileID = FileTable.FileID) 
   INNER JOIN KeywordTable ON FileKeywordTable.KeywordID = KeywordTable.KeywordID 
WHERE
and the remainder (the WHERE clause) is built programmatically depending on user choices, but an example might be:

Code:
 (Keyword = 'thrash' AND 'metal') AND DocType = 'powertab'
While this query will return only power tabs, it will return any power tab that has the keyword 'metal' as well as any power tab that has the keyword 'thrash'. I want it to return only the docs that have both keywords 'thrash' and 'metal'. The brackets seem to make no difference but I've left them in anyway.

Does anybody have any idea what I've done wrong? I've been trawling teh web but no joy. Any help much appreciated
__________________
All practise and no theory
dafuzz is offline   Reply With Quote
Old 03-25-2009, 10:34 AM   #2
Snap
Senior Member
 
Join Date: Mar 2005
Location: Sheffield
Posts: 5,766
Default

hmm, does using "&" or "+" instead of "AND" work?
__________________
www.soundclick.com/geared
Evilmags, in his own true words: "I'm a well known pervert who'd stop at nothing to get my hands on anything female".
Snap is offline   Reply With Quote
Old 03-25-2009, 10:42 AM   #3
dafuzz
Senior Member
 
dafuzz's Avatar
 
Join Date: Feb 2008
Location: SE London
Posts: 3,079
Default

Quote:
Originally Posted by Snap View Post
hmm, does using "&" or "+" instead of "AND" work?
<quick test>

Sadly no

Aren't they concatenation operators in some SQL flavours? I get no hits at all, which would make sense if its searching for 'thrashmetal'.
__________________
All practise and no theory
dafuzz is offline   Reply With Quote
Old 03-25-2009, 10:56 AM   #4
roundthebend
Senior Member
 
roundthebend's Avatar
 
Join Date: Dec 2006
Posts: 2,245
Default

My first thought is that you might need to do a subquery.

I'll see what I can work out.
roundthebend is offline   Reply With Quote
Old 03-25-2009, 10:59 AM   #5
arrowodd
Senior Member
 
arrowodd's Avatar
 
Join Date: Apr 2007
Location: Chippenham
Posts: 371
Default

My sql programming was an age ago, and it's never really been of interest - so I could be way off mark here. in fact "inner join" is new to me.

(its in my head, but hard to explain)
It looks to me that by selecting from the intersection table [FileKeywordTable] which is word|file , however that 'and' works , you're only looking at individual keyword/file correspondancies and wont be able combine conditions.

I think some sort of selecting from filetable where exists [select from intersection: keyword = word1 and fileid joins] and exists [select from interstion: keyword = word2 and fileid joins] may be worth looking at.
arrowodd is offline   Reply With Quote
Old 03-25-2009, 11:01 AM   #6
roundthebend
Senior Member
 
roundthebend's Avatar
 
Join Date: Dec 2006
Posts: 2,245
Default

Code:
SELECT F.FileName
FROM FileTable as F

where 'grunge' in
	(SELECT K.Keyword
	 FROM KeywordTable as K
	 INNER JOIN FileKeywordTable as FK ON FK.KeywordID = K.KeywordID
	 AND FK.FileID = F.FileID)


and 'lyrics' in
	(SELECT K.Keyword
	 FROM KeywordTable as K
	 INNER JOIN FileKeywordTable as FK ON FK.KeywordID = K.KeywordID
	 AND FK.FileID = F.FileID)
roundthebend is offline   Reply With Quote
Old 03-25-2009, 11:02 AM   #7
roundthebend
Senior Member
 
roundthebend's Avatar
 
Join Date: Dec 2006
Posts: 2,245
Default

Although I think my query works, I think you should take arrowodd's use of the EXISTS keyword and rework it. I think it's a better practise for performance reasons.
roundthebend is offline   Reply With Quote
Old 03-25-2009, 11:04 AM   #8
roundthebend
Senior Member
 
roundthebend's Avatar
 
Join Date: Dec 2006
Posts: 2,245
Default

Code:
use FileStoreTest
SELECT F.FileName
FROM FileTable as F

--where 'grunge' in
where EXISTS
	(SELECT K.Keyword
	 FROM KeywordTable as K
	 INNER JOIN FileKeywordTable as FK ON FK.KeywordID = K.KeywordID
	 AND FK.FileID = F.FileID
	 AND K.Keyword = 'grunge')


--and 'lyrics' in
and EXISTS
	(SELECT K.Keyword
	 FROM KeywordTable as K
	 INNER JOIN FileKeywordTable as FK ON FK.KeywordID = K.KeywordID
	 AND FK.FileID = F.FileID
	 AND K.Keyword = 'lyrics')
roundthebend is offline   Reply With Quote
Old 03-25-2009, 11:31 AM   #9
dafuzz
Senior Member
 
dafuzz's Avatar
 
Join Date: Feb 2008
Location: SE London
Posts: 3,079
Default

Thanks peeps! I'm just making the necessary changes now

It never ceases to amaze me the breadth and depth of knowledge on this forum. We should challenge the Eggheads
__________________
All practise and no theory
dafuzz is offline   Reply With Quote
Old 03-25-2009, 11:33 AM   #10
Snap
Senior Member
 
Join Date: Mar 2005
Location: Sheffield
Posts: 5,766
Default

man, we really are a bunch of geeks. Concatentation - thought so in hindsight.

I have patchy knowledge......
__________________
www.soundclick.com/geared
Evilmags, in his own true words: "I'm a well known pervert who'd stop at nothing to get my hands on anything female".
Snap is offline   Reply With Quote
Reply


Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 10:17 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.