通过不唯一的 id 将数据连接在一起
假设我有两个表,如下所示:
mysql> show columns from ping;
+------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+----------------+
| ping_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| msg_size | int(10) unsigned | YES | | NULL | |
| version | int(10) unsigned | YES | | NULL | |
| num_points | int(10) unsigned | YES | | NULL | |
| lat_deg | float | YES | | NULL | |
| lat_min | float | YES | | NULL | |
| long_deg | float | YES | | NULL | |
| long_min | float | YES | | NULL | |
| bearing | float | YES | | NULL | |
| pitch | float | YES | | NULL | |
| roll | float | YES | | NULL | |
| heave | float | YES | | NULL | |
| tide | float | YES | | NULL | |
| hour | int(10) unsigned | YES | | NULL | |
| min | int(10) unsigned | YES | | NULL | |
| sec | int(10) unsigned | YES | | NULL | |
| day | int(10) unsigned | YES | | NULL | |
| month | int(10) unsigned | YES | | NULL | |
| year | int(10) unsigned | YES | | NULL | |
| posx | float | YES | | NULL | |
| posy | float | YES | | NULL | |
| pingtime | int(10) unsigned | YES | | NULL | |
+------------+------------------+------+-----+---------+----------------+
22 rows in set (0.02 sec)
并且:
mysql> show columns from point;
+----------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+---------+----------------+
| point_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| ping_id | int(10) unsigned | NO | | NULL | |
| x | float | NO | | NULL | |
| y | float | NO | | NULL | |
| texture | int(10) unsigned | NO | | NULL | |
| fish | int(10) unsigned | NO | | NULL | |
+----------+------------------+------+-----+---------+----------------+
表 ping 的 Ping_id 是唯一的。然而点的 ping_id 却不是。 IE 每个 ping_id 有几个点。我需要选择数据,以便每个 ping 有几个 x,y 列(点数不是恒定的)。 IE,我正在寻找如下所示的输出:
+----------+------------------+-----------+-----+-----+----+------+----+----+-----+----+
| ping_id | lat_deg | lon_deg | x1 | y1 | x2 | y2 | x3 | y3 | x4 | y4 |
+----------+------------------+------------+-----+-----+----+-----+----+----+----+-----+
Say I have two tables as outlined below:
mysql> show columns from ping;
+------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+----------------+
| ping_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| msg_size | int(10) unsigned | YES | | NULL | |
| version | int(10) unsigned | YES | | NULL | |
| num_points | int(10) unsigned | YES | | NULL | |
| lat_deg | float | YES | | NULL | |
| lat_min | float | YES | | NULL | |
| long_deg | float | YES | | NULL | |
| long_min | float | YES | | NULL | |
| bearing | float | YES | | NULL | |
| pitch | float | YES | | NULL | |
| roll | float | YES | | NULL | |
| heave | float | YES | | NULL | |
| tide | float | YES | | NULL | |
| hour | int(10) unsigned | YES | | NULL | |
| min | int(10) unsigned | YES | | NULL | |
| sec | int(10) unsigned | YES | | NULL | |
| day | int(10) unsigned | YES | | NULL | |
| month | int(10) unsigned | YES | | NULL | |
| year | int(10) unsigned | YES | | NULL | |
| posx | float | YES | | NULL | |
| posy | float | YES | | NULL | |
| pingtime | int(10) unsigned | YES | | NULL | |
+------------+------------------+------+-----+---------+----------------+
22 rows in set (0.02 sec)
and:
mysql> show columns from point;
+----------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+---------+----------------+
| point_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| ping_id | int(10) unsigned | NO | | NULL | |
| x | float | NO | | NULL | |
| y | float | NO | | NULL | |
| texture | int(10) unsigned | NO | | NULL | |
| fish | int(10) unsigned | NO | | NULL | |
+----------+------------------+------+-----+---------+----------------+
Ping_id for the table ping is unique. However ping_id for point is not. IE there are several points for each ping_id. I need to select data so that there is several x,y columns for each ping (number of points are not constant). IE, I am looking for an output that looks like this:
+----------+------------------+-----------+-----+-----+----+------+----+----+-----+----+
| ping_id | lat_deg | lon_deg | x1 | y1 | x2 | y2 | x3 | y3 | x4 | y4 |
+----------+------------------+------------+-----+-----+----+-----+----+----+----+-----+
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该连接表:
并处理结果以获得分组点,例如在 PHP 中:
You should join tables:
and process results to get grouped points for example in PHP:
SELECT * FROM point JOIN ping on point.ping_id = point.ping_id WHERE point.ping_id = ?
SELECT * FROM point JOIN ping on point.ping_id = point.ping_id WHERE point.ping_id = ?