1

これら 3 つのクエリを結合するにはどうすればよいですか?

select gameid, type, Player1, Player2, Player3, Player4, Player5 
from tbl_game 
where Player1=" + userid + " OR Player2=" + userid + " OR Player3=" + userid + 
      " OR Player4=" + userid + " OR Player5=" + userid + " AND Complete = 'No' 

select Player1, Player2, Streaks 
from tbl_streaks 
where gameid = [gameid from first query]

select userid, userid, Facebookid, points 
from tbl_userinfo 
where userid = [players from first query]

useridパラメータとして渡されます。

わからない..

私を助けてください。

4

3 に答える 3

1
select tg.gameid, tg.type, tg.Player1, tg.Player2, tg.Player3, tg.Player4
     , tg.Player5, ts.Streaks, tu.userid, tu.Facebookid, tu.points 
  from tbl_game tg 
 inner join tbl_streaks ts 
    on ts.gameid = tg.gameid 
 inner join tbl_userinfo tu 
    on ( tg.Player1 = tu.userid 
          or tg.Player2 = tu.userid 
          or tg.Player3 = tu.userid 
          or tg.Player4 = tu.userid 
          or tg.Player5 = tu.userid
             )
 where tu.userid = " + userid + " 
   and tg.Complete = 'No';

これはテーブル構造によるものですが、パフォーマンスが向上した最適化されたクエリを作成できるように、スキーマに取り組むことをお勧めします。列プレーヤーを tbl_game から削除し、ユーザー ID を tbl_userinfo に参照して、プレーヤー用に別のルックアップ テーブルを作成します。

于 2012-06-02T10:10:49.660 に答える
1

これはあなたが望むことをするはずです:

select g.gameid, g.type, g.Player1, g.Player2, g.Player3, g.Player4, g.Player5, s.Player1, s.Player2, s.Streaks, u.userid, u.Facebookid, u.points
FROM tbl_game g
JOIN ( select Player1, Player2, Streaks from tbl_streaks) s ON s.gameid = g.gameid
JOIN ( select userid, userid, Facebookid, points from tbl_userinfo ) u ON u.userid = (g.Player1 OR g.Player2 OR g.Player3 OR g.Player4 OR g.Player5)
where Player1=" + userid + " OR Player2=" + userid + " OR Player3=" + userid + 
      " OR Player4=" + userid + " OR Player5=" + userid + " AND Complete = 'No' 

ただし、n プレーヤーの列がたくさんあるのは悪い設計です。専用のテーブルの行にする必要があります ...

于 2012-06-02T08:29:35.427 に答える
0
select gameid , type ,Player1,Player2,Player3,Player4,Player5 from tbl_game 
Natural Join
(select Player1, Player2, Streaks from tbl_streaks) as t4 on t4.gameid=tbl_game.gameid 
Natural Join
(select userid, userid,Facebookid,points from tbl_userinfo) as t5 on t5.userid="+userid+" 
where Player1=" + userid + " OR Player2=" + userid + " OR Player3=" + userid + " OR Player4=" +  userid + " OR Player5=" + userid + " AND Complete = 'No' 
于 2012-06-02T08:24:52.090 に答える