如何从4个表中选择数据

时间:2015-09-08 11:48:18

标签: mysql codeigniter join

我想使用Codeigniter框架从4个表中选择数据。这4个表具有相似的列结构。我想得到与某年和月相对应的表格数据。

这是我的表结构:

t1

accid     uid      month    year        ccbalance
--------------------------------------------------------
101         19       May      1996        4545
-----------------------------------------------------
101         19       sept      1998         1500
--------------------------------------------------------

t2

accid     uid      month      year        insbalance
--------------------------------------------------------
102         19       May       1995         2059
-----------------------------------------------------
102         19       july       1998         2500
--------------------------------------------------------

t3

accid     uid      month    year        ccbalance
--------------------------------------------------------
109         19       June      1999         10000
-----------------------------------------------------
109         19       Aug       1990        1500
--------------------------------------------------------

t4

accid     uid      month    year        ccbalance
--------------------------------------------------------
105         19       Aug      1995         10000
-----------------------------------------------------
105        19       May       1995         3333
--------------------------------------------------------

如果我选择May 1995,我想得到这个结果:

accid     uid      month    year        ccbalance
--------------------------------------------------------
105         19       May     1995         3333

102         19       May     1995         2059

1 个答案:

答案 0 :(得分:0)

Codeigniter 2.0和3.0中没有内置联合函数

您可以创建自己的SQL查询并执行它:

$sql="(SELECT * from t1 where month='May' AND year=1995)
       UNION
      (SELECT * from t2 where month='May' AND year=1995)";
$query = $this->db->query($sql);    
return $query->result();

有关mysql union语法的更多信息,请找here(官方文档)