来自2个表

时间:2015-11-09 13:29:52

标签: php mysql

我有2个表格如下:

表1:common_member

+-----------+-----------+
| uid       | username  |
+-----------+-----------+
| 1         | haha      |
| 2         | walao     |
| 3         | alamak    |
| 4         | hero      |
| 5         | theone    |
| 6         | nobody    |
+-----------+-----------+

表2:labour_slog

+--------------+-------------+--------------+-------------+
| uid          | slaveid     | masterid     | bytime      |
+--------------+-------------+--------------+-------------+
| 1            | 2           | 3            | 123456      |
| 4            | 5           | 6            | 456789      |
+--------------+-------------+--------------+-------------+

我以下面的脚本获取数据:

$queryLabourSlog = DB::query("SELECT * FROM ".DB::table('labour_slog')." ORDER BY id desc");
    while($rowLabourSlog = DB::fetch($queryLabourSlog)) {
        $user_list_slog[] = $rowLabourSlog;
    };
    array_multisort($idss, SORT_DESC, $user_list_slog);

在我的HTML中,我使用

<!--{loop $user_list_slog $value}-->{$value[uid]} on {$value[bytime]} forced hire {$value[masterid]}'s employee {$value[slaveid]}.<!--{/loop}-->

html将显示:

1 on 123456 forced hire 3's employee 2.
4 on 456789 forced hire 6's employee 5.

如何加入Table 1的用户名数据以获得如下循环显示?

haha on 123456 forced hire alamak's employee walao.
hero on 456789 forced hire nobody's employee theone.

感谢。

2 个答案:

答案 0 :(得分:2)

SELECT table_slog.*, u1.`name`, u2.`name` FROM `table_slog` LEFT JOIN `common_member` AS u1 ON `table_slog`.`uid`=u1.`uid` LEFT JOIN `common_member` AS u2 ON `table_slog`.`slaveid`=u2.`uid` LEFT JOIN `common_member` AS u3 ON `table_slog`.`masterid`=u3.uid ORDER BY `id` DESC

答案 1 :(得分:0)

类似的东西:

git