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 > Data Access, Manipulation & Batch Languages > ANSI SQL > Need a second opinion

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-16-07, 19:05
Jym Jym is offline
Registered User
 
Join Date: Jun 2005
Posts: 30
Need a second opinion

I'm not very "efficient" at SQL so I'd like to know if this is correct for what I am doing. To make it simple I have one database (2 identcal but attached)
Table nut_list(nut VARCHAR)
Table ing_list(ing VARCHAR)
Table nut_values(n_RowID, i_RowID)
--------------
the 3 identcal tables with t_ in front of the table name

Table nut_value (and t_nut_value) are the relational table between nut_list and ing_list

What I want to do is select the RowID from nut_value where the relationship between ing_list and nut_list = the relationship between t_ing_list and t_nut_list

Here's some data for example
nut_list 2 records (rowID=1, nut = 'iron') and (rowID = 2, nut = 'calcium')
ing_list 2 records (rowID = 1, ing = 'apple' and (rowID = 2, ing = 'orange')
nut_ing_values
'1' '1'
'2' '1'
'2' '2'
Data tells me
Apple Iron and Apple Calcium
Orange Calcium
tdb.nut_list 3 records (rowID = 27, nut = 'iron') and (rowID = 28, nut = 'sodium') and (rowID = 29, nut = 'calcium')
tdb.ing_list 3 records (rowID = 30, ing = 'apple') and (rowID = 31, ing = 'pear') and (rowID = 32, ing = 'orange')
tdb.nut_ing_values
'27' '30'
'28' '30'
'28' '31'
'29' '32'
So the only 2 records these two databases have in common is Apple - Iron and Orange - Calcium That's what I want selected (OR in other words nut_values.rowID )

So I wrote this
Select nut_values.RowID
FROM nut_values, t_nut_values
JOIN ing_list as c
ON c.rowid = nut_values.i_rowID
JOIN nut_list as b
ON b.rowid = nut_values.n_rowID
WHERE
b.rowID = (
Select nut_list.RowID From nut_list
where nut_list.nut = (
Select nut_list.nut from nut_list, t_nut_list
WHERE t_nut_values.n_rowID = t_nut_list.rowid and t_nut_list.nut = nut_list.nut
)
)
AND
c.rowid = (
Select ing_list.rowid From ing_list
where ing_list.ing = (
Select ing_list.ing from ing_list, t_ing_list
WHERE t_nut_values.i_rowID = t_ing_list.rowid and t_ing_list.ing = ing_list.ing
)
)


Is that correct ?
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