简体   繁体   中英

SELECT where column1 = column2

I've been trying to accomplish the following for the past few hours without any luck:

$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))" );

Let's say we have team1 and team2. A match is played where Team1 is home and Team2 is away. Another match (row) is stored where team2 is home and team1 is away. I want to select both of the teams with a single query.

No teams are playing themselves, I am trying to get 2 rows, where the values of home_team and away_team is mirrored.

Can anyone please help me on track?

* UPDATE *

The return I am getting is the following:

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
    )

)

Where both Array[0] and Array[2] is the same, they are just mirrored. Can I get rid of the duplicates here? I would love to have just Array[0] or Array[2]. Is this possible?

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 

I doubt there would be any rows where the "home team" is the same as the "away team".

It sounds as if you want to find two rows that match.

Based on the conditions in your query, it sounds like you might want something like this:

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

This assumes you have corresponding rows in the table, eg

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

If there are multiple rows with the same (home,away), you'll get multiple matches. For example, with:

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

You'd get back a total of twelve rows. (Rows with id values of 2 and 11 will each get "matched" to rows with id values of 3, 5, and 7.)


UPDATE

Getting rid of duplicates depends on the source of the duplication. Adding a DISTINCT keyword will ensure that no two rows in the result set are exactly the same, but I suspect your duplicates issue is deeper than that... where the bears and tigers have faced each other, home and away, in multiple league matches.

In that case, you'd need something additional in the tables, and some predicate to limit the matches. This could be date, and some method to get the "latest date", but that would depend on what else is in the table.

With just the columns shown, a GROUP BY and an aggregate function like MAX() can be used to get one distinct row for each "match".

For example:

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

Note that returning the home and away from t2 is redundant, since t1.home = t2.away etc. The values from t1 and t2 are the same, except that home and away are swapped.

To limit the "inverse" rows, so you'd get (bears,tigers) but not (tigers,bears) , you could specify an additional predicate so you only get one "side" of the inverse:

AND t1.league_match_home_team < t2.league_match_home_team

FOLLOWUP

(There was a typo error in my query, the first JOIN predicate should have specified t2. on the right side. I believe OP found that problem and fixed it.)

Based on the latest update, to eliminate the "mirror" inverse rows in the resultset, you could add a predicate like this (following the GROUP BY clause if your query has one.)

  HAVING t1_id < t2_id

(A HAVING clause can reference aliases assigned to the return columns, unlike the WHERE clause.)

If you don't have a GROUP BY in your query, you're likely to get better performance with a WHERE clause:

WHERE t1.match_id < t2.match_id

If it doesn't matter which of the two rows you get, then it doesn't matter whether it's a less than or a greater than comparison. It doesn't matter which of the t1 and t2 columns you choose to compare ("id", "home" or "away"), all that is required is a comparison of a column between t1 and t2 that are guaranteed to be different (so you'll get just one side of the mirror.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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