-1

I'm trying to select data from my android sqlite database. I have 3 tables - table Players (list of all players), table Matches (list of all matches) and table PlayersMatches (list of players in each match, created as many to many relationship). I want to write SELECT command, which returns all matches, where played two players together. Here is a little example:

Table PLAYERS
-------------
id    name
1     John
2     Jamie
3     Joe

Table MATCHES
-------------
id    date
1     17/12/01
2     17/12/02
3     17/12/03

Table PLAYERS_MATCHES
---------------------
id    id_Player    id_Match
1     1            1
2     2            1
3     1            2
4     3            2
5     1            3
6     2            3

So, I want to write select command, which return all matches, where played John (1) and Jamie (2). Result should be:

SELECT RETURNED
-------------
id_match
1
3

Note, that there can be more than 2 players in each match.

It is possible to write a single sql command or I should do it in different way? (for example select command for the first player, another for the second player and programatically in Android filter theese matches).

Gnudiff
  • 4,297
  • 1
  • 24
  • 25
buci
  • 15
  • 1
  • 2

1 Answers1

1

It is perfectly OK to do it in single sql. This is the kind of queries SQL was built for.

You do a self join on PlayerMatches table like:

SELECT distinct M1.id_match
From PLAYERS_MATCHES M1 CROSS JOIN PLAYERS_MATCHES M2  
WHERE M1.id_match=M2.id_match AND M1.id_player=1 AND M2.id_player=2

Verification:

sqlite> select * from matches;
id          p           m
----------  ----------  ----------
1           1           1
2           2           1
3           1           2
4           3           2
5           1           3
6           2           3
sqlite>

sqlite> select distinct m1.m from matches m1 cross join matches m2
   ...> where m1.m=m2.m and m1.p=1 and m2.p=2;
m
----------
1
3
sqlite>
Gnudiff
  • 4,297
  • 1
  • 24
  • 25
  • This is not valid SQL. Did you want to use USING? – CL. Dec 09 '17 at 17:46
  • @CL. Sorry, I took syntax from another implementation. Fixed. – Gnudiff Dec 09 '17 at 18:06
  • @buci aliases for tables, defined when you list them. – Gnudiff Dec 09 '17 at 18:07
  • @Gnudiff, are you sure that your command is correct? when I tried it, it returned only one match (with id = 1), but it should return two matches (id 1 and 3) – buci Dec 09 '17 at 18:15
  • @buci I am sorry, I will try to look at it from computer, atm am at mobile phone only, hard to check. The only place it could be different is you could have to do `from PLAYERS_MATCHES M1 cross join PLAYERS_MATCHES M2` instead. – Gnudiff Dec 09 '17 at 18:31
  • @Gnudiff, I tried to change according your comment, but it didn't help. I will wait for your another advice, when you will be at computer – buci Dec 09 '17 at 18:39
  • @buci i updated my answer and checked it in sqlite3 with your PlayerMatches table -- worked for me. – Gnudiff Dec 09 '17 at 18:49
  • @Gnudiff, I'm so sorry, it looks like a some bug in DB Browser for SQLite - I worked with database file and everytime it was returning only one match. I create a new database file with same values and now, it returns two matches, that it is correct. Thank you for your time and patience :) – buci Dec 09 '17 at 19:12