PHP Codeigniter MySQL 查询 - 将 4 个表连接在一起,其中 3 个表使用每个表中的一列分组

Posted

技术标签:

【中文标题】PHP Codeigniter MySQL 查询 - 将 4 个表连接在一起,其中 3 个表使用每个表中的一列分组【英文标题】:PHP Codeigniter MySQL query - join 4 tables together, with 3 tables using group by one column from each table 【发布时间】:2020-09-04 17:06:54 【问题描述】:

物品表

+----+------+
| id | name |
+----+------+
|  1 | abc  |
|  2 | def  |
|  3 | ghi  |
+----+------+

购买表

+------+-------------+-------+---------+
| b_id | b_date      | b_qty | b_itmid |
+------+-------------+-------+---------+
|  1   | 2020-05-01  |  10   |    1    |
|  2   | 2020-05-01  |  20   |    1    |
|  3   | 2020-05-02  |  5    |    2    |
|  3   | 2020-05-03  |  10   |    3    |
+------+-------------+-------+---------+

租桌子

+------+-------------+-------+---------+
| r_id | r_date      | r_qty | r_itmid |
+------+-------------+-------+---------+
|  1   | 2020-05-03  |   5   |    2    |
|  2   | 2020-05-03  |   10  |    2    |
|  3   | 2020-05-04  |   15  |    3    |
+------+-----------+---------+---------+

销售表

+------+-------------+-------+---------+
| s_id | s_date      | s_qty | s_itmid |
+------+-------------+-------+---------+
|  1   | 2020-05-03  |  10   |    1    |
|  2   | 2020-05-05  |  20   |    3    |
|  3   | 2020-05-06  |  5    |    3    |
+------+-----------+---------+---------+

我正在尝试使用php foreach 获得类似这样的输出...

$trans_date
$buy_qty
$rent_qty
$sell_qty
$item

如果项目 ID 1

+-------------+--------------+---------------+---------------+------+
| trans_date  |   buy_qty    |    rent_qty   |   sell_qty    | item |
+-------------+--------------+---------------+---------------+------+
| 2020-05-01  |       30     |       0       |       0       | abc  |
| 2020-05-02  |       0      |       0       |       0       | abc  |
| 2020-05-03  |       0      |       0       |       10      | abc  |
| 2020-05-04  |       0      |       0       |       0       | abc  |
| 2020-05-05  |       0      |       0       |       0       | abc  |
| 2020-05-06  |       0      |       0       |       0       | abc  |
+-------------+--------------+---------------+---------------+------+

这是我对一张表的查询(b_date 列有时间戳值)...

$query  = $this->db->query("
SELECT FROM_UNIXTIME(b_date,'%d %M') AS date_b
     , SUM(b_qty) AS qty_b 
  FROM buytable 
 WHERE b_itmid = 1
   AND MONTH(FROM_UNIXTIME(b_date)) = MONTH(CURDATE()) 
 GROUP 
    BY DATE(FROM_UNIXTIME(b_date))
");

        if ($query->num_rows() > 0) 
            foreach ($query->result() as $data) 
                $result[] = $data;
            
            return $result;
        

【问题讨论】:

请edit 提出您的问题,并附上您尝试过但对您不起作用的内容。 @Dave 我已经包含了一张表的作品查询 【参考方案1】:

终于找到了正确答案https://***.com/a/61875506/7554875

select d.date, b.qty_buy, r.qty_rent, s.qty_sell, i.name
    from items i
    cross join (
        select b_date as date from buy 
        union all select r_date as date from rent 
        union all select s_date as date from sell
    ) d
    left join (select b_date as date, b_itmid, sum(b_qty) qty_buy  
    from buy  group by date, b_itmid) b
        on b.date = d.date and b.b_itmid = i.id
    left join (select r_date as date, r_itmid, sum(r_qty) qty_rent 
    from rent group by date, r_itmid) r
        on r.date = d.date and r.r_itmid = i.id
    left join (select s_date as date, s_itmid, sum(s_qty) qty_sell 
    from sell group by date, s_itmid) s
        on s.date = d.date and s.b_itmid = i.id
    where i.id = 1 and d.date >= date_format(curent_date, '%Y-%m-01')
    order by d.date

【讨论】:

以上是关于PHP Codeigniter MySQL 查询 - 将 4 个表连接在一起,其中 3 个表使用每个表中的一列分组的主要内容,如果未能解决你的问题,请参考以下文章

Mysql 查询在 Codeigniter 中返回空结果,但在本机 PHP 中工作正常

更新查询使 Codeigniter PHP 中 MySQL 的记录为空

在 php codeigniter 中使用 join/concat 的 mysql 查询结果

PHP Codeigniter MySQL 查询 - 将 4 个表连接在一起,其中 3 个表使用每个表中的一列分组

SQL 基准测试:PHP ActiveRecord ORM 与 MySQL 与 CodeIgniter Active Record 与标准 PHP

从 codeigniter mysql 下载 csv