如何使用数据库中的三个表中的一个公共字段来连接它们.(Mysql)


How to join three tables from a database using just one common field amongst them all.(Mysql)

这是的三个表

1. table `p_transactions` has following fields
(`txn_id`, `txn_uid`, `txn_bid_no`, `txn_date`, `txn_desc`, `txn_amt`, `txn_fee`, `txn_mode`, `txn_status`, `txn_mdate`)
2 . table `p_game_results` has following fields.
(`id`, `game_id`, `game_combo`, `game_combo_hr`, `cdate`, `mdate`)
3. Table `p_game_room_results` has following fields
 (`id`, `game_id`, `room_id`, `txn`, `round`, `result`, `score`, `cdate`, `mdate`)

我想加入他们使用他们的txn id作为一个公共字段。

这是我试过的东西。但不确定,我肯定是错的。

$sql    = "SELECT * FROM " . $prefix . "_user_game_results".$prefix."_game_room_results".$prefix."_transactions WHERE". $prefix . "_user_game_result.uid"='$prefix."_game_room_results.id"'. and .$prefix."_transactions.txn_uid"='$prefix."_game_room_results.uid"'"";
        $result = $this->sql_fetchrowset($this->sql_query($sql));

谢谢。

这应该做到:

select * from p_transactions pt 
inner join p_game_room_results pgrr on pgrr.txn=pt.txn_id 
inner join p_game_results pgr on pgr.game_id=pgrr.game_id

按公用字段联接所有表。CCD_ 1和CCD_。

    select * 
    from p_transactions as p_t INNER JOIN p_game_results as p_g_r
    ON p_t.txn_id = p_g_r.id
    INNER JOIN
    p_game_room_results as p_g_r_r
    ON p_g_r_r.game_id = p_g_r.game_id;

以下别名用于INNER JOIN

   p_transactions as p_t
   p_game_results as p_g_r
   p_game_room_results as p_g_r_r

我建议您阅读这些有用的SQL Joins教程

http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html

http://en.wikipedia.org/wiki/Join_%28SQL%29