Question

I have basic tables one and two. Let`s call them tbl1 and tbl2.

tbl1
---------
1 - apple |
2 - orange |
3 - banana |
4 - pineapple

tbl2
---------
1 - table |
2 - chair |
3 - sofa |

Then there is tbl3 that has foreign keys that link it to both tables above.

Table 3 form has two select fields: one that queries tbl1 and another that queries tbl2. So far, so good. I combine items from tbl1 and tbl2 in tbl3.

Then I have the following situation: When users fulfill tbl3, I want the select2 (corresponding to data from tbl2) to reload after the user has picked an item from tbl1. What for?

Let`s say that for the first time fulfilling tbl3 the user has picked "apple" from tbl1 and "sofa" from tbl2. When the user selected "apple", the second dropdownlist reloaded with all the 3 items as options.

Now, the second time the user fulfills a tbl3 form, if the user selects "apple" again, he will now have only 2 options. apple AND sofa have been picked before. Options now are only "table" and "chair". If he picks "apple" and "table" now, the remaining option for first item "apple" will be "chair". And so on...

I cannot think of an SQL to run this query that grabs elements from tbl2. I must use the selected element from tbl1 and somehow grab the REMAINING items, those that do not correspond to a mach. Is this SQL query possibe? I believe so, but cannot think of a way out.

Was it helpful?

Solution

I am guessing that table3 has columns:

User, Tbl1-ID, Tbl2-ID

I am guessing that you want, for user X, the values for tbl2 based on the selecton made from tbl1 that are NOT in table 3.

SELECT ID, Value
FROM tbl2
WHERE tbl2.ID NOT IN (
   SELECT Tbl2-ID
   FROM Table3
   WHERE (User = <this user>) AND (Tbl1-ID = <selected tbl1 ID>)
   )

OTHER TIPS

If the links are on a per user basis:

SELECT t.id,
       t.value
  FROM TABLE2 t
  JOIN TABLE3 t3 ON t3.id != t.id

If the links are are per user basis for:

SQL Server:

SELECT t.id,
       t.value
  FROM TABLE2 t
  JOIN TABLE3 t3 ON t3.id != t.id AND t3.userid = @userId

Oracle:

SELECT t.id,
       t.value
  FROM TABLE2 t
  JOIN TABLE3 t3 ON t3.id != t.id AND t3.userid = :userId
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top