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

时间:2011-12-09 13:06:24

标签: mysql pivot one-to-many

说我有两张桌子如下所示:

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 | 
+----------+------------------+------------+-----+-----+----+-----+----+----+----+-----+

2 个答案:

答案 0 :(得分:1)

你应该加入表格:

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;
}

答案 1 :(得分:0)

SELECT * FROM点JOIN ping on point.ping_id = point.ping_id WHERE point.ping_id =?