tags:

views:

5407

answers:

5

I have 2 tables. One (domains) has domain ids, and domain names (dom_id, dom_url).

the other contains actual data, 2 of which columns require a TO and FROM domain names. So I have 2 columns rev_dom_from and rev_dom_for, both of which store the domain name id, from the domains table.

Simple.

Now I need to actually display both domain names on the webpage. I know how to display one or the other, via the LEFT JOIN domains ON reviews.rev_dom_for = domains.dom_url query, and then you echo out the dom_url, which would echo out the domain name in the rev_dom_for column.

But how would I make it echo out the 2nd domain name, in the dom_rev_from column?

+14  A: 
Stephen Wrighton
the trick is that you've identified each JOIN with a name 'AS to' and 'AS from' so that you can use them in the SELECT.
MattSmith
using a non keyword for a table name would clarify. in addition, use the "as" keyword when aliasing.
TheSoftwareJedi
Forgive me, but I still dont understand how this is supposed to work. The 2 tables are: domains (dom_id, dom_url), and reviews (rev_id, rev_dom_from, rev_dom_for). If someone could write the exact query so I cna get the hang of it, that would be great, because I have no idea what I have to edit.
Yegor
Think of the aliases as referencing rows in the table, not the table itself. By analogy, in a loop like "for(i=0;i<max;i++)" the variable i is an iterating value, it's not the loop itself.
Bill Karwin
A: 

Given the following tables..

Domain Table
dom_id | dom_url

Review Table
rev_id | rev_dom_from | rev_dom_for

Try this sql... (It's pretty much the same thing that Stephen Wrighton wrote above) The trick is that you are basically selecting from the domain table twice in the same query and joining the results.

Select d1.dom_url, d2.dom_id from
review r, domain d1, domain d2
where d1.dom_id = r.rev_dom_from
and d2.dom_id = r.rev_dom_for

If you are still stuck, please be more specific with exactly it is that you don't understand.

delux247
A: 

I know this post is old but after a number of searches I finnaly found this post at the top of a google search and the first reply was exactly what i needed.

Aulus
A: 

I was searching for this exact answer. Thanks a lot...

Aries
Welcome to Stack Overflow... Note that this type of comment would normally be posted as a comment to an answer or to the original question, and not as an answer to the question. Otherwise you risk that the answer gets downvoted.
Daniel Vassallo
A: 

Thanks for both answers. I combined them and got my solution.

ookpalm