Codeigniter使用where子句连接多个表

时间:2015-02-25 16:50:29

标签: php mysql codeigniter mysqli

我有四个表需要连接在一起,其中一个表有两个不同的记录我需要抓取。 guildparty表我获取了这些表的名称,以便我可以向用户显示名称而不是ID,charlog表具有char_id&#39 ; s我想要显示的创建日期和最后登录日期。以下是表格:

char表:

----------------------------------------------------
| char_id    | name    | guild_id    | party_id    |
|---------------------------------------------------
| 150000     | char1   | 3           | 3           |
| 150001     | char2   | 2           | (NULL)      |
| 150002     | char3   | (NULL)      | 1           |
| 150003     | char4   | 1           | 2           |
----------------------------------------------------

guild表:

-------------------------
| guild_id   | name     |
-------------------------
| 1          | guild_1  |
| 2          | guild_2  |
| 3          | guild_3  |
-------------------------

party表:

-------------------------
| party_id   | name     |
-------------------------
| 1          | party_1  |
| 2          | party_2  |
| 3          | party_3  |
-------------------------

charlog表:

----------------------------------------------------
| time                 | char_id   | char_msg      |
----------------------------------------------------
| 2015-02-14 06:45:32  | 150000    | make new char |
| 2015-02-14 06:45:58  | 150000    | char select   |
| 2015-02-15 12:32:19  | 150001    | make new char |
| 2015-02-15 16:54:01  | 150000    | char select   |
| 2015-02-15 19:23:54  | 150001    | char select   |
| 2015-02-16 01:32:13  | 150002    | make new char |
| 2015-02-16 01:33:01  | 150003    | make new char |
| 2015-02-16 04:45:43  | 150000    | char select   |
| 2015-02-16 07:43:22  | 150003    | char select   |
----------------------------------------------------

如上所述,我需要从make new char表中获取charlog条目以显示创建角色的时间以及最后显示的LAST(按日期)char select条目角色被播放,所有这一切都只有一个char_id。

总而言之,我正在寻找一个看起来像这样的表:

---------------------------------------------------------------------------------------------------------------------
| char_id    | name     | guild_id | guild_name | party_id | party_name | create_time         | lastlogin_time      |
---------------------------------------------------------------------------------------------------------------------
| 150000     | char1    | 3        | guild_3    | 3        | party_3    | 2015-02-14 06:45:32 | 2015-02-16 04:45:43 |

我在Codeigniter中使用以下活动记录行来尝试获取我需要的数据。它正确返回create_date但不返回lastlogin_time(它是空白的):

function get_char_info($cid) {
    $this->db->select('char.*,guild.guild_id,guild.name AS guild_name,party.party_id,party.name AS party_name,charlog1.time AS create_time,charlog2.time AS lastlogin_time');
    $this->db->from('char');
    $this->db->where('char.char_id', $cid);
    $this->db->join('guild', 'char.guild_id = guild.guild_id', 'left');
    $this->db->join('party', 'char.party_id = party.party_id', 'left');
    $this->db->join('charlog AS charlog1', 'char.char_id = charlog1.char_id AND charlog1.char_msg = "make new char"', 'left');
    $this->db->join('charlog AS charlog2', 'char.char_id = charlog2.char_id AND charlog2.char_msg = (SELECT max(charlog.time) FROM charlog WHERE char_msg = "char select")', 'left');
    $query = $this->db->get();
    return $query->row();
}

如上所述,guild_name,party_name和create_time正确通过,但lastlogin_time为空,没有错误。

我已尝试在活动记录条款中混淆了一些内容,但无法显示lastlogin_time。任何帮助,将不胜感激。

1 个答案:

答案 0 :(得分:1)

子查询上的连接不正确:

应该是charlog2.time = (SELECT....)而不是charlog2.char_msg = (SELECT....)