問題描述
我有兩張桌子.一個(域)有域 id 和域名(dom_id、dom_url).
I have 2 tables. One (domains) has domain ids, and domain names (dom_id, dom_url).
另一個包含實際數據,其中 2 列需要 TO 和 FROM 域名.所以我有 2 列 rev_dom_from 和 rev_dom_for,它們都存儲域表中的域名 id.
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.
簡單.
現在我需要在網頁上實際顯示兩個域名.我知道如何通過 LEFT JOIN 域 ON review.rev_dom_for = domain.dom_url 查詢來顯示一個或另一個,然后您回顯 dom_url,這將回顯 rev_dom_for 列中的域名.
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.
但是我如何讓它在 dom_rev_from 列中與第二個域名相呼應?
But how would I make it echo out the 2nd domain name, in the dom_rev_from column?
推薦答案
你會使用另一個連接,大致如下:
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
編輯:
您所做的只是多次加入表格.查看帖子中的查詢:它從評論表(別名為 rvw)中選擇值,該表為您提供了對域表的 2 個引用(一個 FOR 和一個 FROM).
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).
此時,將 Domain 表左連接到 Reviews 表是一件簡單的事情.一次(別名為 toD)用于 FOR,第二次(別名為 fromD)用于 FROM.
At this point it's a simple matter to left join the Domain table to the Reviews table. Once (aliased as toD) for the FOR, and a second time (aliased as fromD) for the FROM.
然后在 SELECT 列表中,您將從 DOMAIN 表的兩個 LEFT JOINS 中選擇 DOM_URL 字段,通過引用域表的每個聯接的表別名來引用它們,并將它們別名為 ToURL 和 FromUrl.
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.
有關 SQL 中別名的更多信息,請閱讀此處.
For more info about aliasing in SQL, read here.
這篇關于你如何在 mysql 中加入同一個表,兩次?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!