PDO :: FETCH_COLUMN | PDO :: FETCH_GROUP在三列上

时间:2017-12-11 12:56:31

标签: php mysql pdo

我们可以使用PDO::FETCH_COLUMN | PDO::FETCH_GROUP这样的

来获取city_id和temp列
 city_id      temp   date
   1          24.3    2017-12-03
   1          35.2    2017-11-26
   1          24.4    2017-11-19
   1          28.0    2017-11-12
   2          4.0     2017-12-03
   2          2.0     2017-11-26
   2          6.0     2017-11-19
   2          7.0     2017-11-12
   .
   .

 $stmt = $pdo->query("SELECT `city_id`,`temp` FROM cities");
 $rows = $stmt->fetchAll(PDO::FETCH_COLUMN | PDO::FETCH_GROUP);

$rows将是这样的

[
  1 => [24.3,35.2,24.4,28.0],
  2=>  [4.0,2.0,6.0,7.0],
   ....
  ]

但是有可能以某种方式获得一个关联的多维数组,其中将代表所有三列。像这样的东西

 $stmt = $pdo->query("SELECT `city_id`,date,`temp` FROM cities");
 $rows = $stmt->fetchAll(What should be here? so that);

$rows应该是这样的

 [
  1 => [2017-12-03 => 24.3, 2017-11-26=>35.2, 2017-11-19=>24.4, 2017-11-12=>28.0],
  2=>  [2017-12-03 => 4.0, 2017-11-26=>2.0, 2017-11-19=>6.0, 2017-11-12=>7.0],
   ....
  ]

0 个答案:

没有答案