[英]SELECT where column1 = 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的值被鏡像。
有人可以幫助我嗎?
*更新*
我得到的回報如下:
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
)
)
如果Array [0]和Array [2]相同,則它們只是鏡像。 我可以擺脫這里的重復嗎? 我希望只有Array [0]或Array [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
我懷疑“主隊”與“客隊”是否會存在任何相同的行。
聽起來好像要查找匹配的兩行。
根據查詢中的條件,聽起來您可能想要這樣的東西:
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
如果有多個具有相同(家,客場)的行,則將獲得多個匹配項。 例如,使用:
id home away
-- ----- ------
2 bears tigers
3 tigers bears
5 tigers bears
7 tigers bears
11 bears tigers
您總共將獲得十二行。 (ID值為2和11的行將分別與ID值為3、5和7的行“匹配”。)
UPDATE
消除重復項取決於重復項的來源。 添加DISTINCT
關鍵字將確保結果集中沒有任何兩行完全相同,但是我懷疑您的重復問題比那更深。。。在多次聯賽中, bears
和tigers
在本國和tigers
面對對方。
在這種情況下,您需要在表中添加一些其他內容,並需要一些謂詞來限制匹配項。 這可能是日期,並且是獲取“最新日期”的某種方法,但這取決於表中的其他內容。
僅顯示了各列,即可使用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
需要注意的是返回home
,並away
從T2是多余的,因為t1.home = t2.away
等從值t1
和t2
是相同的,不同之處在於home
和away
被交換。
要限制“反”行,這樣您將獲得(bears,tigers)
但不會獲得(tigers,bears)
,您可以指定一個附加謂詞,以便僅獲得反面的“一側”:
AND t1.league_match_home_team < t2.league_match_home_team
跟進
(我的查詢中有一個拼寫錯誤,第一個JOIN謂詞應在右側指定t2.
。我相信OP找到了該問題並已解決。)
基於最新更新,要消除結果集中的“鏡像”反行,您可以添加這樣的謂詞(如果查詢有一個,則遵循GROUP BY
子句。)
HAVING t1_id < t2_id
(與WHERE
子句不同, HAVING
子句可以引用分配給返回列的別名。)
如果查詢中沒有GROUP BY
,則使用WHERE子句可能會獲得更好的性能:
WHERE t1.match_id < t2.match_id
如果獲得的兩行中的哪一條都不重要,則小於還是大於比較都無關緊要。 選擇要比較的t1和t2列中的哪一個(“ id”,“ home”或“ away”)都沒有關系,所需要做的只是保證t1和t2之間的列比較不同(所以您只會得到鏡子的一側。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.