SELECT a non ID column in a foreign key table (TABLE B) based on the foreign key in the primary table (TABLE

The Complex One

I have two tables, a STUDENT and RULES table. The student table holds data related to students and the rules table holds rules related to a particular student. Now on the student table, I have 5 columns as foreign keys to the rule table e.g

CREATE TABLE dbo.RULES
(
    ID int identity not null primary key,
    RULENAME varchar
)

CREATE TABLE dbo.STUDENT
(
    ID int identity not null primary key,
    NAME varchar(50),
    SURNAME varchar(50),
    ADRESS varchar(50),
    RULE1 int not null references dbo.RULES(ID),
    RULE2 int not null references dbo.RULES(ID),
    RULE3 int not null references dbo.RULES(ID),
    RULE4 int not null references dbo.RULES(ID),
    RULE5 int not null references dbo.RULES(ID)
)

What I want to achieve is selecting NAME, SURNAME, ADDRESS from student table and join it with the RULENAME for each RULE foreign key in STUDENT e.g

NAME SURNAME ADDRESS RULE1NAME RULE2NAME RULE3NAME RULE4NAME RULE5NAME

I stayed up the whole night trying to crack this but sadly I'm still at square one. I will attribute that to my lack of experience in SQL but ya, can someone pretty pretty please help me?

UPDATE

Thanks for your answers guys, dotnetom, Bharadwaj.

I inner joined 5 times as suggested on dotnetoms answer. The query works fine but it returns the same rule for all five rules. Here's my query

SELECT 
STUDENT.NAME, 
STUDENT.SURNAME, 
STUDENT.ADDRESS, 
RULES.RULENAME AS RULE1NAME, 
RULES.RULENAME AS RULE2NAME, 
RULES.RULENAME AS RULE3NAME, 
RULES.RULENAME AS RULE4NAME, 
RULES.RULENAME AS RULE5NAME
FROM STUDENT s 
INNER JOIN RULES AS r1 ON STUDENT.RULE1 = RULES.ID
INNER JOIN RULES AS r2 ON STUDENT.RULE2 = RULES.ID
INNER JOIN RULES AS r3 ON STUDENT.RULE3 = RULES.ID
INNER JOIN RULES AS r4 ON STUDENT.RULE4 = RULES.ID
INNER JOIN RULES AS r5 ON STUDENT.RULE5 = RULES.ID

any suggestions? Thanks!

dotnetom

You should be able to use this query:

SELECT 
    s.NAME, 
    s.SURNAME, 
    s.ADDRESS, 
    r1.RULENAME AS RULE1NAME, 
    r2.RULENAME AS RULE2NAME, 
    r3.RULENAME AS RULE3NAME, 
    r4.RULENAME AS RULE4NAME, 
    r5.RULENAME AS RULE5NAME
FROM STUDENT s 
    INNER JOIN RULES r1 ON s.RULE1 = r1.ID
    INNER JOIN RULES r2 ON s.RULE2 = r2.ID
    INNER JOIN RULES r3 ON s.RULE3 = r3.ID
    INNER JOIN RULES r4 ON s.RULE4 = r4.ID
    INNER JOIN RULES r5 ON s.RULE5 = r5.ID

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Finding Primary Table for a Foreign Key

From Dev

Relating two foreign key columns in Table A to a primary key column in Table B

From Dev

Mapping of primary key as foreign key to another table

From Dev

create a foreign key on a primary key of another table

From Dev

Use of Primary Key as Foreign Key in Foreign Key Table

From Dev

Select query on a Table based on condition from other table ? No Foreign key

From Dev

How to save auto generated primary key Id in foreign key column in same table

From Dev

How to insert Primary Key value of the primary table to the Foreign Key column of the child table in MySQL?

From Dev

Join on foreign key of table with the max primary key of that table

From Dev

mysql join table where foreign key is primary key of same table

From Dev

Update primary key in one table which is foreign key in another table

From Dev

Foreign key to table A and B, where A already have a foreign key to B

From Dev

How to refer primary key as Foreign to various table

From Dev

Primary And Foreign key mapping between view and table

From Dev

Inserting a foreign key into a table

From Dev

Add Foreign Key to table

From Dev

updating a table with foreign key

From Dev

Foreign key on same table

From Dev

MySql table with Foreign key

From Dev

foreign key not creating in table

From Dev

Updating a foreign key of a table

From Dev

Add Foreign Key to table

From Dev

updating a table with foreign key

From Dev

Insert foreign key into table

From Dev

Foreign key in the first table

From Dev

deleting table with a foreign key

From Dev

Insert a new column into the Foreign Key table when a particular column in the primary key table is Update (Using Triggers)

From Dev

Update Column Of All Rows In A Table From Another Table Using Primary Key And Foreign Key

From Dev

How to display the last inserted ID (primary key) of a table in a html textbox that is in a relationship with a foreign key of another table in mvc

Related Related

  1. 1

    Finding Primary Table for a Foreign Key

  2. 2

    Relating two foreign key columns in Table A to a primary key column in Table B

  3. 3

    Mapping of primary key as foreign key to another table

  4. 4

    create a foreign key on a primary key of another table

  5. 5

    Use of Primary Key as Foreign Key in Foreign Key Table

  6. 6

    Select query on a Table based on condition from other table ? No Foreign key

  7. 7

    How to save auto generated primary key Id in foreign key column in same table

  8. 8

    How to insert Primary Key value of the primary table to the Foreign Key column of the child table in MySQL?

  9. 9

    Join on foreign key of table with the max primary key of that table

  10. 10

    mysql join table where foreign key is primary key of same table

  11. 11

    Update primary key in one table which is foreign key in another table

  12. 12

    Foreign key to table A and B, where A already have a foreign key to B

  13. 13

    How to refer primary key as Foreign to various table

  14. 14

    Primary And Foreign key mapping between view and table

  15. 15

    Inserting a foreign key into a table

  16. 16

    Add Foreign Key to table

  17. 17

    updating a table with foreign key

  18. 18

    Foreign key on same table

  19. 19

    MySql table with Foreign key

  20. 20

    foreign key not creating in table

  21. 21

    Updating a foreign key of a table

  22. 22

    Add Foreign Key to table

  23. 23

    updating a table with foreign key

  24. 24

    Insert foreign key into table

  25. 25

    Foreign key in the first table

  26. 26

    deleting table with a foreign key

  27. 27

    Insert a new column into the Foreign Key table when a particular column in the primary key table is Update (Using Triggers)

  28. 28

    Update Column Of All Rows In A Table From Another Table Using Primary Key And Foreign Key

  29. 29

    How to display the last inserted ID (primary key) of a table in a html textbox that is in a relationship with a foreign key of another table in mvc

HotTag

Archive