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 回答