0

我搜索并试图找到一个星期的解决方案,但我被卡住了。我试图在一个表中选择两个不同的列并将它们与另一个数据库进行比较。这是我的设置:

Table 1: fighters
  fightr_id     fightr_namn         fightr_record   fightr_nick
  1             Brock Lesnar        5-3-0 (0)       Brockan
  2             Frank Mir           5-1-0 (0)       Fettot
  3             Martin Johansson    2-33-1 (0)  
  4             Mirko Filipovic     12-22-0 (0)     cro cop

这是我想要作为“大师”的表:

Table 2: Matches
  match_id  gala_id     fightr_id1  fightr_id2   match_order
  3         14          1           2            0
  4         14          3           4            1

我想用“战斗机”表中的名称来输出“fightr_id1”,以及“战斗机”表中的“fightr_id2”作为名称。

这是我试图接受的输出:

<?php while( $row = mysql_fetch_assoc( $document_get)) { ?>
    <tr><td><?php echo $row['match_ordning']; ?> </td> 
    <td>Fighter 1: <?php echo $row['fightr_name']; ?></td>
    <td>Fighter 2: <?php echo $row['fightr_namn']; ?></td></tr>

<?php } ?>

我试图阅读有关加入 SQL 的信息,我认为这可能是要走的路。但我似乎无法弄清楚如何编码选择查询。你能帮助我吗?

先感谢您。

4

1 回答 1

1
select 
  m.match_id, 
  f1.fightr_namn as fighter1Name, 
  f2.fightr_namn as fighter2Name 
from matches m
inner join fighters f1 on m.fightr_id1 = f1.fightr_id
inner join fighters f2 on m.fightr_id2 = f2.fightr_id

http://sqlfiddle.com/#!2/31875f/3

于 2013-10-26T23:30:20.113 回答