skip to Main Content

I have 3 tables in a MySQL database

user

user_id, (PK)
email

agreement_user

agreement_user_id, (PK)
user_id, (FK user.user_id)
details

contract

contract_id, (PK)
agreement_user_id, (FK agreement_user.agreement_user_id)

details

What should be the query to get contract.details and user.email given a contract_id

2

Answers


  1. maybe you can try

    SELECT contract.details, user.email FROM contract
    INNER JOIN agreement_user ON contract.agreement_user_id = agreement_user.agreement_user_id
    INNER JOIN user ON agreement_user.user_id = user.user_id
    WHERE contract.contract_id = @yourSpecificId;
    
    Login or Signup to reply.
  2. Try this

       SELECT u.email, c.details FROM contract AS co
       JOIN agreement_user AS au ON co.agreement_user_id = au.agreement_user_id
       JOIN user AS u ON au.user_id = u.user_id
       WHERE co.contract_id = @contractId
    

    Replace @contractId with your value

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search