See if two table rows exist, inner joined to another row

James

Imagine I've got a sport where I've got a table, MATCH, and a table PLAYER, and the table MATCH_PLAYER. The MATCH_PLAYER table obviously links a certain match to certain players, and in the table MATCH_PLAYER I store information such as the position played, number of points scored etc. by each player. The MATCH table contains which team won, what date it was played etc.

I can make queries such as "what is the % win rate when player_id plays" using this, when you supply the player ID:

    SELECT
                    sum(case when m.win_left = mp.left then 1 else 0 end) * 100.00
                        / count(*) as win_percentage
    FROM
                    match m
                    inner join match_player mp
                        on mp.match_id = m.id
    WHERE mp.player_id = [whatever]

However, what I don't know is how to say "what is the % win rate when player_id x and player_id y play together".

This is obviously a fundamental idea behind databases but I can't for the life of me figure out the terminology for what I want to do, and so searching for it has been hard.

Tom H

Simply join to the table twice - once for each player:

SELECT
    SUM(CASE WHEN (m.win_left = mp.left) AND (M.win_dire = MP2.left) THEN 1 ELSE 0 END) * 100.00
        / COUNT(*) AS win_percentage
FROM
    Match M
INNER JOIN Match_Player MP1 ON
    MP1.match_id = M.id AND
    MP1.player_id = [whatever]
INNER JOIN Match_Player MP2 ON
    MP2.match_id = M.id AND
    MP2.player_id = [whatever #2]

If you're going to go beyond two or three players then there are some other approaches through set matching that might be more efficient as the number of players gets larger, but for your case this will likely be fastest.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MySQL select row with two matching joined rows from another table

From Dev

How to insert one row of a table into two rows of another table

From Dev

T-SQL convert inner joined table's rows as columns

From Dev

Search Row not exist in another table

From Dev

Selecting two rows from another table using one row

From Dev

See if multiple entries exist in another table

From Dev

Using max() to get the latest rows in a table, joined on another table

From Dev

Finding % of rows in a table that exist in another table?

From Dev

Split table row into two rows

From Dev

Inner join with one row of another table

From Dev

MySQL Show rows that exist in one table but not it another

From Dev

MySQL Show rows that exist in one table but not it another

From Dev

Synchronizing two joined table

From Dev

how to use a column alias when selecting * from a table inner joined with another table

From Dev

join table with another joined table

From Dev

How to get value of another row if row does not exist in table?

From Dev

Delete rows in a table if an identifier doesn't exist in another table

From Dev

Insert on each inner joined foreach row matched

From Dev

Select rows according row type in another table

From Dev

Adding rows to a table based on number in another row

From Dev

Get rows that are not logged in a joined table

From Dev

Sum of unique rows of joined table

From Dev

Get rows that are not logged in a joined table

From Dev

delete row from table where column does not exist in another table

From Dev

Changing table row color per two rows

From Dev

Changing table row color per two rows

From Dev

SQL Row Concatenation on joined table

From Dev

getting top row of joined table

From Dev

How to select a single row where multiple rows exist from a table

Related Related

  1. 1

    MySQL select row with two matching joined rows from another table

  2. 2

    How to insert one row of a table into two rows of another table

  3. 3

    T-SQL convert inner joined table's rows as columns

  4. 4

    Search Row not exist in another table

  5. 5

    Selecting two rows from another table using one row

  6. 6

    See if multiple entries exist in another table

  7. 7

    Using max() to get the latest rows in a table, joined on another table

  8. 8

    Finding % of rows in a table that exist in another table?

  9. 9

    Split table row into two rows

  10. 10

    Inner join with one row of another table

  11. 11

    MySQL Show rows that exist in one table but not it another

  12. 12

    MySQL Show rows that exist in one table but not it another

  13. 13

    Synchronizing two joined table

  14. 14

    how to use a column alias when selecting * from a table inner joined with another table

  15. 15

    join table with another joined table

  16. 16

    How to get value of another row if row does not exist in table?

  17. 17

    Delete rows in a table if an identifier doesn't exist in another table

  18. 18

    Insert on each inner joined foreach row matched

  19. 19

    Select rows according row type in another table

  20. 20

    Adding rows to a table based on number in another row

  21. 21

    Get rows that are not logged in a joined table

  22. 22

    Sum of unique rows of joined table

  23. 23

    Get rows that are not logged in a joined table

  24. 24

    delete row from table where column does not exist in another table

  25. 25

    Changing table row color per two rows

  26. 26

    Changing table row color per two rows

  27. 27

    SQL Row Concatenation on joined table

  28. 28

    getting top row of joined table

  29. 29

    How to select a single row where multiple rows exist from a table

HotTag

Archive