Selecting rows from the parent table only if multiple rows in the Children table match

I am creating code that teaches tic tac toe by storing information in a database. I have two tables, Games(ID,Winner)and Turns(ID,Turn,GameID,Place,Shape). I want to find a parent by several child data.
For example:

SELECT GameID FROM Turns WHERE
GameID IN (WHEN Turn = 1 THEN Place = 1) AND GameID IN (WHEN Turn = 2 THEN Place = 4);

Is this possible?
I am using ms access.

Turm - Game turns
GameID - Game identifier
Place - Place on the matrix 1 = upper right, 9 = lower left
Shape - X or circle

Thanks in advance

+4
source share
4 answers

, , ().

SELECT T.GameID
FROM Turns AS T
WHERE
   (T.Turn = 1 AND T.Place = 1)
   OR (T.Turn = 2 AND T.Place = 4)
GROUP BY T.GameID
HAVING Count(*) = 2;

, , .

, , !

+3

, Turns:

SELECT * FROM Games
WHERE GameID IN 
(
    SELECT Turns1.GameID
    FROM Turns AS Turns1 
    INNER JOIN Turns AS Turns2 
    ON Turns1.GameID = Turns2.GameID
    WHERE (
        (Turns1.Turn=1 AND Turns1.Place = 1) 
        AND 
        (Turns2.Turn=2 AND Turns2.Place = 4))
);

Self Join of Turns (aliased Turns1 Turns2) , :

WHERE (
        (Turns.Turn=1 AND Turns.Place = 1) 
        AND 
        (Turns.Turn=2 AND Turns.Place = 4))

. , .

Access , SQL View , Query Designer. , .

+1
select GameID from Games g where exists (select * from turns t where 
t.gameid = g.gameId and ((turn =1 and place = 1) or (turn =2 and place =5)))

, , , .

: http://www.techonthenet.com/sql/exists.php

-2

, , : "154728" . ,

-2

Source: https://habr.com/ru/post/1629249/


All Articles