doushantun0614 2011-09-23 05:45
浏览 167
已采纳

如何使用其中的一个公共字段从数据库连接三个表。(Mysql)

These are three tables

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`)

I would like to join them using their txn id as a common field.

here's something I tried. but not sure, I'm sure its wrong .

$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));

Thanks.

  • 写回答

2条回答 默认 最新

  • dongrong7883 2011-09-23 06:53
    关注
        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;
    

    The following alias are used in INNER JOIN

       p_transactions as p_t
       p_game_results as p_g_r
       p_game_room_results as p_g_r_r
    

    I suggest you to go through these useful SQL Joins Tutorial

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

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计