在DATA

时间:2017-08-05 01:53:08

标签: php mysql count

我的数据库中有这样的表格

lp_kon

ID | lpkonName
1  | kon1
2  | kon2

lp_seb

ID | ID_lpkon | lpsebName
1  |    1     |  seb1
2  |    2     |  seb3

lp_rek

ID | ID_lpseb | lprekName
1  |   1      | rek1
2  |   1      | rek2

lp_tl

ID | ID_lprek | lptlName
1  |    1     | tl1
2  |    1     | tl2

我使用此查询加入这4个表

SELECT
    a.id as id_lpkon,
    b.id as id_seb,
    c.id as id_rek,
    d.id as id_tl
FROM `lp_kon` a 
left OUTER JOIN lp_seb b  ON a.id = b.ID_lpkon
LEFT OUTER join lp_rek c  on b.id = c.ID_lpseb
LEFT OUTER join lp_tl  d  on c.id = d.ID_lprek
ORDER by a.id ASC

我得到了这样的支持

id_lpkon|id_lpseb|id_lprek|id_lptl|
1       |  1     |  1     |  1    |
1       |  1     |  1     |  2    |
1       |  1     |  2     | NULL  |
1       |  2     | NULL   | NULL  |
2       |  Null  | NULL   | NUll  |

如何在分组时计算表格行。比如下面的结果

id_lpkon | count(lpkon) | id_seb | count(lpseb | id_rek | count(lprek) | id_lptl | count(lptl)   
1        |     4        |    1   |    3        |   1    |   2          |1        | 1       | 
1        |     4        |    1   |    3        |   1    |   2          |2        | 1       | 
1        |     4        |    1   |    3        |   2    |   1          |NUll     |NUll     |
1        |     4        |    2   |    1        |  NULL  | NULL         |NULL     |NULL     |
2        |     1        |  NULL  |   NULL      |  NULL  | NULL         |NULL     |

对于每一行,每个关系表都有一个计数行

使用查询更新

Lp_kon

CREATE TABLE `lp_kon` ( `ID` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, `lpkonName` text NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1
INSERT INTO `lp_kon` (`ID`, `lpkonName`) VALUES (NULL, 'kon1'), (NULL, 'kon2');

Lp_seb

CREATE TABLE `lp_seb` ( `ID` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,`ID_lpkon` text NOT NULL, `lpsebName` text NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1
INSERT INTO `lp_seb` (`ID`,`ID_lpkon`, `lpsebName`) VALUES (NULL,`1`,'seb1'), (NULL,`2`,'seb3');

Lp_rek

  CREATE TABLE `lp_rek` ( `ID` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,`ID_lpseb` text NOT NULL, `lprekName` text NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1
INSERT INTO `lp_rek` (`ID`,`ID_lpkon`, `lprekName`) VALUES (NULL,`1`,'rek1'), (NULL,`1`,'rek2');

Lp_tl

  CREATE TABLE `lp_tl` ( `ID` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,`ID_lprek` text NOT NULL, `lptlName` text NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1
INSERT INTO `lp_tl` (`ID`,`ID_lprek`, `lpsebName`) VALUES (NULL,`1`,'tl1'), (NULL,`1`,'tl2');

3 个答案:

答案 0 :(得分:1)

我认为使用PHP更容易解决这个问题。

使用您的查询并将数据提取到关联数组中。我应该这样:

$dbData = [
    ['id_lpkon' => 1, 'id_lpseb' => 1,    'id_lprek' => 1,    'id_lptl'  => 1],
    ['id_lpkon' => 1, 'id_lpseb' => 1,    'id_lprek' => 1,    'id_lptl'  => 2],
    ['id_lpkon' => 1, 'id_lpseb' => 1,    'id_lprek' => 2,    'id_lptl'  => null],
    ['id_lpkon' => 1, 'id_lpseb' => 2,    'id_lprek' => null, 'id_lptl'  => null,],
    ['id_lpkon' => 2, 'id_lpseb' => null, 'id_lprek' => null, 'id_lptl'  => null,]
];

现在使用嵌套的foreach循环来计算所有提取的值:

$columns = ['id_lpkon', 'id_lpseb', 'id_lprek', 'id_lptl'];
$counts = [];
foreach ($columns as $column) {
    $counts[$column] = [];
}

foreach ($dbData as $row) {
    foreach ($row as $column => $value) {
        if ($value === null) {
            continue;
        } elseif (isset($counts[$column][$value])) {
            $counts[$column][$value]++;
        } else {
            $counts[$column][$value] = 1;
        }
    }
}

您可以在此处停止,因为您已在$counts数组中拥有所需数据,如下所示:

$counts = [
  'id_lpkon' => [1 => 4, 2 => 1],
  'id_lpseb' => [1 => 3, 2 => 1],
  'id_lprek' => [1 => 2, 2 => 1],
  'id_lptl'  => [1 => 1, 2 => 1],
];

但是如果您需要一个“表”中的数据,请使用下一个嵌套的foreach循环:

foreach ($dbData as $rn => $row) {
    foreach ($row as $column => $value) {
        $countColumn = $column . '_count';
        if ($value === null) {
            $dbData[$rn][$countColumn] = null;
        } else {
            $dbData[$rn][$countColumn] = $counts[$column][$value];
        }
    }
}

演示:http://rextester.com/XQTSX69507

答案 1 :(得分:0)

我相信sum()正是您所寻找的。

答案 2 :(得分:0)

我相信,因为每当他需要计算每个人的价值时,他就会这样做。

COUNT(表)AS标题, a.ID AS id_lpkon, COUNT(表)AS Header2, b.ID AS id_seb,

等等