Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

sql - Query for retrieving data from a bridging table of many to many relationship


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

So you're trying to link e.g. Case to Referral via the association table Case_Referral? So what's the issue you're facing?

This is a pretty simple, straightforward SQL statement - SELECT from Case, join on Case_Referral via the case_id key, then join to Referral using the referral_id, and specify which columns from each table you need:

SELECT
    c.user_name, c.date as CaseDate, c.priority, c.case_status,
    r.date AS ReferralDate, r.referral_name
FROM
    dbo.Case c
INNER JOIN
    dbo.Case_Referral cr ON c.case_ID = cr.case_ID
INNER JOIN
    dbo.Referral r ON cr.referral_ID = r.referral_ID

So what is the issue / problem you're not understanding?

You can use the same "technique" to join the other m:n relationships.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...