Need your help with sql joins

5 pts.
Tags:
MySQL
Hi there, I have two tables named articles and writers. articles table looks like this : article_id writer_id translator_id article_text writers table looks like this: id writer_name date_joined A few of the articles could be translated but mostly self written articles. The writer_id and translator_id are from writers table. A writer could write an article as well as translate an article. How could I query for all articles that has writers name and translators name if that article has a translator too. eg: artciles: 1,1,2,"How to do it"; 2,1,0,"my job is this"; writers: 1,"Richard","2008-01-10"; 2,"Ponni","2008-01-20"; pls help me with the problem. thanks. b.m

Answer Wiki

Thanks. We'll let you know when a new response is added.

You will want to join to the writers table twice. Once with an inner join for the author, and once with a left outer join for the translator.

<pre>SELECT articles.article_id, writers.writer_name, trans.writer_name as translator_name
FROM articles
INNER JOIN writers on artciles.writer_id = writers.id
LEFT OUTER JOIN writers as trans ON articles.translator_id = trans.id</pre>

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following