0

Here are the tables I have:

Table A which has entries with "item" and "grade" fields
Table B which has entries with A.id
Tuple table B-C

I want all the A entries that have item= "x" and grade = "y" And all the C entries that are associated with a B entry that is associated with an A entry that has item = "x" and grade = "y"

For example

A table:

    A.item = "x", A.Grade = "y", A.id = 1
    A.item = "x", A.Grade = "y", A.id = 2
    A.item = "x", A.Grade = "y", A.id = 3
    A.item = "r", A.Grade = "z", A.id = 4

B Table

   B.AID = 1, B.id = 10
   B.AID = 1, B.id = 11
   B.AID = 2, B.id = 13
   B.AID = 3, B.id = 14
   B.AID = 4, B.id = 15

B-C Tuple Table

   BID = 10, CID = 20
   BID = 11, CID = 20
   BID = 13, CID = 20
   BID = 15, CID = 21

The query should return all the entries in the A table and the entry 20 but not 21 in the C table because C.id = 21 is only tupled with a B that is associated with an A that does not meet the item and grade requirements.

7
  • What have you tried? We are glad to help with problems, but this looks like a bit like you want us to do your work. Commented Sep 14, 2012 at 0:49
  • i have tried a lot of things. Im still confused on some complex sql queries. I built this entire database and now i need some help on complex queries. Your help is very appreciated Commented Sep 14, 2012 at 1:02
  • Add your best query to the question, please. Commented Sep 14, 2012 at 1:15
  • Its only going to confuse you. I have abstracted it in the question. I just need to be pointed in the right direction with these types of queries. I dont understand why you are giving me a hard time. Commented Sep 14, 2012 at 1:33
  • 1
    Some CREATE TABLE statements and some INSERTs for some sample data would help a lot. So would an expected result. Commented Sep 14, 2012 at 1:35

1 Answer 1

0

The associations, while sounding complicated in written form, are just a simple join among three tables: a joins to b joins to c.

You identify how the columns need to be joined: "a B entry that is associated with an A entry", and looking at the columns sounds like you want to join on b.aid = a.id. Similarly for b and c.

SELECT ...
FROM
    a
    JOIN b ON b.aid = a.id
    JOIN b_c ON b_c.bid = b.id
WHERE
    ...

This constructs the original dataset before it was split into the three normalised tables.

The next step is to filter by the given conditions. You only want rows where " item = "x" and grade = "y"", so add those to WHERE clause prefixing them with the table name, which is optional in this case):

WHERE
    a.item = 'x'
    AND a.grade = 'y'

Finally, you can pick which columns you really need, in the SELECT clause. I'm gussing SELECT b_c.cid would do. Though if you also have a c table you might want to join on that table, too, and select columns from it.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes. Exactly right. Im starting to see how it works a little better. Thank you for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.