从mysql数据集中过滤数据

时间:2020-06-08 15:56:43

标签: mysql aggregate geospatial

我一般对数据库和SQL还是陌生的。我正在使用MySQL数据库,并从site中了解北京的GPS轨迹,因此按所述创建了这些表。

CREATE TABLE `plt` (
  `directory` varchar(10) DEFAULT NULL,
  `latitude` double DEFAULT NULL,
  `longitude` double DEFAULT NULL,
  `flag` int(11) DEFAULT NULL,
  `altitude` double DEFAULT NULL,
  `passeddate` varchar(255) DEFAULT NULL,
  `gpsdate` date DEFAULT NULL,
  `gpstime` time DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `plt_distinct` (
  `directory` varchar(10) NOT NULL DEFAULT '',
  `latitude` double NOT NULL DEFAULT '0',
  `longitude` double NOT NULL DEFAULT '0',
  `flag` int(11) DEFAULT NULL,
  `altitude` double NOT NULL DEFAULT '0',
  `passeddate` varchar(255) DEFAULT NULL,
  `gpsdate` date NOT NULL DEFAULT '0000-00-00',
  `gpstime` time NOT NULL DEFAULT '00:00:00',
  `gpsdatetime` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`directory`,`latitude`,`longitude`,`gpsdate`,`gpstime`,`altitude`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE `labels` (
  `directory` varchar(10) NOT NULL DEFAULT '',
  `starttime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `endtime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  `transportationmode` varchar(10) NOT NULL DEFAULT '',
  PRIMARY KEY (`directory`,`starttime`,`endtime`,`transportationmode`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

我这是这些表中当前的内容:

mysql> SELECT * FROM plt_distinct LIMIT 5;
+-----------+----------+-----------+------+----------+------------+------------+----------+---------------------+
| directory | latitude | longitude | flag | altitude | passeddate | gpsdate    | gpstime  | gpsdatetime         |
+-----------+----------+-----------+------+----------+------------+------------+----------+---------------------+
| 000       |        0 | 39.999686 |  116 |        0 | 164        | 0000-00-00 | 00:20:08 | 0000-00-00 00:00:00 |
| 000       |        0 | 39.999693 |  116 |        0 | 164        | 0000-00-00 | 00:20:08 | 0000-00-00 00:00:00 |
| 000       |        0 | 39.999702 |  116 |        0 | 163        | 0000-00-00 | 00:20:08 | 0000-00-00 00:00:00 |
| 000       |        0 | 39.999703 |  116 |        0 | 162        | 0000-00-00 | 00:20:08 | 0000-00-00 00:00:00 |
| 000       |        0 | 39.999709 |  116 |        0 | 165        | 0000-00-00 | 00:20:08 | 0000-00-00 00:00:00 |
+-----------+----------+-----------+------+----------+------------+------------+----------+---------------------+
5 rows in set (0.00 sec)

mysql> SELECT * FROM labels LIMIT 5;
+-----------+---------------------+---------------------+--------------------+
| directory | starttime           | endtime             | transportationmode |
+-----------+---------------------+---------------------+--------------------+
| 010       | 2007-06-26 11:32:29 | 2007-06-26 11:40:29 | bus                |
| 010       | 2008-03-28 14:52:54 | 2008-03-28 15:59:59 | train              |
| 010       | 2008-03-28 16:00:00 | 2008-03-28 22:02:00 | train              |
| 010       | 2008-03-29 01:27:50 | 2008-03-29 15:59:59 | train              |
| 010       | 2008-03-29 16:00:00 | 2008-03-30 15:59:59 | train              |
+-----------+---------------------+---------------------+--------------------+
5 rows in set (0.00 sec)

然后,我要计算每个transportationmode的GPS点数。我该怎么做?

1 个答案:

答案 0 :(得分:-1)

请多做一些。还发布您的预期输出数据。 据我所知,您可以使用此查询。

从标签GROUP BY标签total_transportationmode中选择count(目录)作为total_transportationmode;

相关问题