Sql Server模糊查询

时间:2018-01-21 20:08:11

标签: mysql sql sql-server database

1)总玩家(player_id,player_name)

2)chess_player(chess_id,player_id)

3)tennis_player(tennis_id,player_id)

4)游戏(chess_id,tennis_id,date)

现在游戏介于棋手和网球运动员之间。

如何通过SQL查询检索数据: -

player_name(国际象棋)| player_name(网球)|日期

例如: - TotalPlayers

           player_id        player_name

      1)    ply1            John

      2)    ply2            Rohn
                           Chess_Players

           chess_id        player_id

      1)    chess1           ply1
                           Tennis_Player

           chess_id        player_id

      1)    tennis1           ply2
                      Game

           chess_id        tennis_id            date

      1)    chess1           tennis1          12/12/2018

预期产量: -                        PLAYER_NAME(CHESS)PLAYER_NAME(网球)日期

                       JOHN                  ROHN                 12/12/2018

2 个答案:

答案 0 :(得分:0)

为了在同一个表中获得两个名称存储,您可以使用不同的别名

两次使用同一个表
select t1.player_name as chess_player_name, t2.player_name as tennis_player_name, 
from game
inner join chess_player cp on cp.chess_id = game.chess_id
inner join tennis_player tp on tp.tennis_id = game.tennis_id
inner join Totalplayers t1 = t1.player_id = cp_player_id
inner join Totalplayers t2 = t2.player_id = tp_player_id

答案 1 :(得分:0)

  

由于国际象棋和网球运动员的名字都存储在一张桌子上,   那么如何在一行中分别检索他们的个人名字?

您使用同一张桌子两次,但每张牌都有一个不同的别名