-6

I have to make a SQL statement which is taking values from three tables one table is table with users other with opinion and the third one is table with subject where are four foregin keys- one of the is pointing to opinion record and the rest as separate keys for users record.

How to select a record from this table which containd subject name, opinion value, subject proffesor and subject opinion guy?

2
  • 5
    Maybe this is just me, but this question is not written well enough to understand and answer. Especially, can you provide more details on your schema (actual columnnames for each table...at least PK and FK) Mar 3, 2012 at 18:28
  • Please add the definitions of the tables
    – rjdevereux
    Mar 3, 2012 at 18:29

1 Answer 1

4

Okay, it's Saturday night and I'm feeling mellow enough to tackle this without a data model.

You have given us the names of the three lookup tables (subjects, opinions, users) but not the actual structures and columns. So I'm making some guesses.

select subjects.name as subject_name
       , opinions.value
       , o_users.name as opinion_guy
       , p_users.name as professor
from  opinions 
      join subjects on ( opinions.subject_id = subjects.id)
      join users as o_users on ( o_users.id = opinions.opinion_guy_id)
      join users as p_users on ( p_users.id = subjects.professor_id)

/

I realize that this query almost certainly doesn't fit your schema but it should give you some clues. Otherwise, please give us the table structures, primary and foreign keys, etc.

Not the answer you're looking for? Browse other questions tagged or ask your own question.