简体   繁体   English

你如何在 mysql 中加入同一个表,两次?

[英]How do you join on the same table, twice, in mysql?

I have 2 tables.我有2张桌子。 One (domains) has domain ids, and domain names (dom_id, dom_url).一个(域)具有域 id 和域名(dom_id、dom_url)。

the other contains actual data, 2 of which columns require a TO and FROM domain names.另一个包含实际数据,其中 2 列需要 TO 和 FROM 域名。 So I have 2 columns rev_dom_from and rev_dom_for, both of which store the domain name id, from the domains table.所以我有 2 列 rev_dom_from 和 rev_dom_for,它们都存储域表中的域名 id。

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.我知道如何通过 LEFT JOIN 域 ON review.rev_dom_for = domain.dom_url 查询来显示一个或另一个,然后您回显 dom_url,这将回显 rev_dom_for 列中的域名。

But how would I make it echo out the 2nd domain name, in the dom_rev_from column?但是我如何让它在 dom_rev_from 列中回显第二个域名?

you'd use another join, something along these lines:你会使用另一个连接,类似这样的东西:

SELECT toD.dom_url AS ToURL, 
    fromD.dom_url AS FromUrl, 
    rvw.*

FROM reviews AS rvw

LEFT JOIN domain AS toD 
    ON toD.Dom_ID = rvw.rev_dom_for

LEFT JOIN domain AS fromD 
    ON fromD.Dom_ID = rvw.rev_dom_from

EDIT :编辑

All you're doing is joining in the table multiple times.您所做的就是多次加入表格。 Look at the query in the post: it selects the values from the Reviews tables (aliased as rvw), that table provides you 2 references to the Domain table (a FOR and a FROM).查看帖子中的查询:它从评论表(别名为 rvw)中选择值,该表为您提供了对域表的 2 个引用(一个 FOR 和一个 FROM)。

At this point it's a simple matter to left join the Domain table to the Reviews table.此时,将 Domain 表左连接到 Reviews 表是一件简单的事情。 Once (aliased as toD) for the FOR, and a second time (aliased as fromD) for the FROM.一次(别名为 toD)用于 FOR,第二次(别名为 fromD)用于 FROM。

Then in the SELECT list, you will select the DOM_URL fields from both LEFT JOINS of the DOMAIN table, referencing them by the table alias for each joined in reference to the Domains table, and alias them as the ToURL and FromUrl.然后在 SELECT 列表中,您将从 DOMAIN 表的两个 LEFT JOINS 中选择 DOM_URL 字段,通过引用域表的每个连接的表别名来引用它们,并将它们别名为 ToURL 和 FromUrl。

For more info about aliasing in SQL, read here .有关 SQL 中别名的更多信息,请阅读此处

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.试试这个 sql...(这与 Stephen Wrighton 上面写的几乎一样) 诀窍是你基本上在同一个查询中从域表中选择两次并加入结果。

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.如果您仍然卡住,请更具体地说明您不明白的地方。

Read this and try, this will help you:阅读并尝试,这将帮助您:

Table1表格1

column11,column12,column13,column14

Table2表2

column21,column22,column23,column24


SELECT table1.column11,table1.column12,table2asnew1.column21,table2asnew2.column21 
FROM table1 INNER JOIN table2 AS table2asnew1 ON table1.column11=table2asnew1.column21  INNER TABLE table2 as table2asnew2 ON table1.column12=table2asnew2.column22

table2asnew1 is an instance of table 2 which is matched by table1.column11=table2asnew1.column21 table2asnew1是表 2 的一个实例,由table1.column11=table2asnew1.column21匹配

and

table2asnew2 is another instance of table 2 which is matched by table1.column12=table2asnew2.column22 table2asnew2是表 2 的另一个实例,由table1.column12=table2asnew2.column22匹配

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM