【问题标题】:How to select data from 4 tables如何从 4 个表中选择数据
【发布时间】:2015-12-04 02:15:17
【问题描述】:

我想使用 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

【问题讨论】:

  • 在 SQL 中,这类似于 (t1 UNION t2 UNION t3 UNION t4) WHERE month="May" AND year=1995,这可能有助于您找到 codeigniter 实现。也很有帮助:stackoverflow.com/questions/2040655/…
  • 看起来您不想要“加入”(收集关于同一项目的不同信息),而是想要一个“联合”(收集来自不同来源的项目的信息)

标签: mysql codeigniter join


【解决方案1】:

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联合语法的信息你可以找到here(官方文档)

【讨论】:

    猜你喜欢
    • 2013-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-18
    • 2023-04-10
    • 1970-01-01
    • 2017-09-21
    • 2014-01-05
    相关资源
    最近更新 更多