2014-04-29 65 views
2

我一直在試圖完成過去幾個小時以下沒有任何的運氣:選擇其中列1 = COLUMN2

$stmt = $db->query("SELECT league_match_id as match_id, league_match_home_team as home_team, league_match_away_team as away_team FROM $table WHERE ((home_team = away_team) AND (away_team = home_team))"); 

比方說,我們有TEAM1和TEAM2。在Team1在家並且Team2不在的情況下進行比賽。另一場比賽(排)存儲在team2在家且team1不在的地方。我想用一個查詢來選擇兩個團隊。

還沒有球隊在打自己,我試圖讓2行,其中HOME_TEAM和AWAY_TEAM的值被鏡像。

任何人都可以幫助我走上正軌嗎?

* UPDATE *

我得到回報如下:

Array 
(
    [0] => Array 
    (
     [t1_id] => 26 
     [t1_home] => 2 
     [t1_away] => 1 
     [t2_id] => 24 
     [t2_home] => 1 
     [t2_away] => 2 
    ) 

    [1] => Array 
    (
     [t1_id] => 28 
     [t1_home] => 3 
     [t1_away] => 1 
     [t2_id] => 25 
     [t2_home] => 1 
     [t2_away] => 3 
    ) 

    [2] => Array 
    (
     [t1_id] => 24 
     [t1_home] => 1 
     [t1_away] => 2 
     [t2_id] => 26 
     [t2_home] => 2 
     [t2_away] => 1 
    ) 

    [3] => Array 
    (
     [t1_id] => 29 
     [t1_home] => 3 
     [t1_away] => 2 
     [t2_id] => 27 
     [t2_home] => 2 
     [t2_away] => 3 
    ) 

    [4] => Array 
    (
     [t1_id] => 25 
     [t1_home] => 1 
     [t1_away] => 3 
     [t2_id] => 28 
     [t2_home] => 3 
     [t2_away] => 1 
    ) 

    [5] => Array 
    (
     [t1_id] => 27 
     [t1_home] => 2 
     [t1_away] => 3 
     [t2_id] => 29 
     [t2_home] => 3 
     [t2_away] => 2 
    ) 

) 

如果雙方陣[0]和陣列[2]是一樣的,他們只是鏡像。我可以擺脫這裏的重複嗎?我很想擁有Array [0]或Array [2]。這可能嗎?

+0

表中數據的一個小例子,以及你想從數據中返回的結果集可以很好地解釋你想要的東西。 – spencer7593

回答

2

我懷疑會有「主隊」與「客隊」相同的任何行。

聽起來好像你想找到匹配行。

根據您查詢的條件,這聽起來像你可能想是這樣的:

SELECT t1.league_match_id   AS t1_match_id 
    , t1.league_match_home_team AS t1_home_team 
    , t1.league_match_away_team AS t1_away_team 
    , t2.league_match_id   AS t2_match_id 
    , t2.league_match_home_team AS t2_home_team 
    , t2.league_match_away_team AS t2_away_team 
    FROM $table t1 
    JOIN $table t2 
    ON t1.league_match_home_team = t2.league_match_away_team 
    AND t1.league_match_away_team = t2.league_match_home_team 

這裏假設你在表中有相應的行,例如

id home away 
-- ----- ------ 
    2 bears tigers 
    3 tigers bears 

如果有多行相同(home,away),則會得到多個匹配項。例如,與:

id home away 
-- ----- ------ 
    2 bears tigers 
    3 tigers bears 
    5 tigers bears 
    7 tigers bears 
11 bears tigers 

你會得到總共十二行。 (具有2和11的ID值的行將各獲得 「匹配」 的行5的3 ID值,和7)


UPDATE

擺脫重複的依賴於重複的來源。添加DISTINCT關鍵字將確保在結果集中沒有兩行是完全一樣的,但我懷疑你的重複的問題比更深......在bearstigers都面臨着對方,主客場,在多個聯賽。

在這種情況下,你需要的東西在表中額外的,和一些謂詞來限制比賽。這可能是日期,以及一些獲取「最新日期」的方法,但這取決於表中的其他內容。

僅顯示列,可以使用GROUP BY和類似MAX()的聚合函數爲每個「匹配」獲取一個不同的行。

例如:

SELECT MAX(t1.league_match_id) AS t1_match_id 
    , t1.league_match_home_team AS t1_home_team 
    , t1.league_match_away_team AS t1_away_team 
    , MAX(t2.league_match_id) AS t2_match_id 
    , t1.league_match_away_team AS t2_home_team 
    , t1.league_match_home_team AS t2_away_team 
    FROM $table t1 
    JOIN $table t2 
    ON t1.league_match_home_team = t2.league_match_away_team 
    AND t1.league_match_away_team = t2.league_match_home_team 
GROUP 
    BY t1.league_match_home_team 
    , t1.league_match_away_team 

注意,從時刻t2返回homeaway是多餘的,因爲t1.home = t2.away等的值從t1t2是相同的,所不同的是homeaway被交換。

要限制「反」行,所以你會得到(bears,tigers)但不(tigers,bears),你可以指定一個額外的謂詞,所以你只能得到一個反向的「方」:

AND t1.league_match_home_team < t2.league_match_home_team 

後續

(有我的查詢中一個錯字的錯誤,第一連接謂詞應該在右側指定t2.。我相信OP發現問題,並修復它。)

基於最新的更新,以消除「鏡子」在結果逆行,你可以添加這樣的謂詞(GROUP BY條款下,如果你的查詢有一個。)

HAVING t1_id < t2_id 

(A 。HAVING條款可以參考分配到返回列的別名,不像WHERE條款)

如果您沒有在您的查詢GROUP BY做,你可能有一個WHERE子句,以獲得更好的性能:

WHERE t1.match_id < t2.match_id 

如果您得到的兩行中的哪一行並不重要,那麼它是小於還是大於比較無關緊要。選擇要比較的t1和t2列中的哪一列(「id」,「home」或「away」)無關緊要,只需要比較t1和t2之間保證不同的列(所以你只會得到鏡子的一面。)

+0

我喜歡這個查詢,它按預期工作,但我得到重複。我如何防止這種情況? – skolind

+0

@Kolind:擺脫重複取決於重複的來源。添加一個DISTINCT關鍵字將 – spencer7593

+0

我已更新我的問題。如果你有時間看看它,我會很高興。再次感謝 :) – skolind

2
SELECT 
    t1.league_match_id , 
    t1.league_match_home_team , 
    t1.league_match_away_team , 
    t2.league_match_id , 
    t2.league_match_home_team, 
    t2.league_match_away_team 
FROM 
    {$table} t1 JOIN {$table} t2 ON t1.league_match_home_team=t2.league_match_away_team as away_team AND 
            t2.league_match_home_team=t1.league_match_away_team as away_team 
GROUP BY 
    t1.league_match_id , 
    t1.league_match_home_team , 
    t1.league_match_away_team , 
    t2.league_match_id , 
    t2.league_match_home_team, 
    t2.league_match_away_team 
+0

感謝您的回答。我更新了我的問題,因爲我想要做的事情有些混亂。您所寫的查詢根本沒有提供任何行。 – skolind

+0

它似乎並沒有像那樣工作。 spencer7593的答案正在工作,但我有重複。 – skolind

+0

是的。我正在用spencer7593的答案。它運作良好。非常感謝你的時間。 – skolind