通过不唯一的 id 将数据连接在一起

发布于 2024-12-20 07:28:07 字数 3238 浏览 1 评论 0原文

假设我有两个表,如下所示:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陈甜 2024-12-27 07:28:07

您应该连接表:

SELECT pg.ping_id, lat_deg, long_deg, p.x, p.y 
FROM ping pg 
JOIN point p ON p.ping_id = pg.ping_id

并处理结果以获得分组点,例如在 PHP 中:

$pings = array();
foreach($results as $row){
  $pings[$row['ping_id']][] = $row;
}

You should join tables:

SELECT pg.ping_id, lat_deg, long_deg, p.x, p.y 
FROM ping pg 
JOIN point p ON p.ping_id = pg.ping_id

and process results to get grouped points for example in PHP:

$pings = array();
foreach($results as $row){
  $pings[$row['ping_id']][] = $row;
}
夏末染殇 2024-12-27 07:28:07

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 = ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文