我在我的 SQL while 循环中得到了这个:
PartID:1 Year:2003 ModelID:1375
PartID:1 Year:2004 ModelID:1375
PartID:1 Year:2005 ModelID:1375
PartID:2 Year:1995 ModelID:1244
PartID:2 Year:1996 ModelID:1244
PartID:2 Year:1997 ModelID:1244
PartID:2 Year:1998 ModelID:1244
PartID:2 Year:1999 ModelID:1244
PartID:2 Year:2000 ModelID:1244
PartID:2 Year:2001 ModelID:1244
PartID:2 Year:1996 ModelID:2361
PartID:2 Year:1997 ModelID:2361
PartID:2 Year:1998 ModelID:2361
PartID:2 Year:1999 ModelID:2361
PartID:2 Year:2000 ModelID:2361
但我需要这样的数据库插入(PartId,Start_Year,End_Year,ModelId):
PartID:1 Start Year:2003 End year:2005 ModelID:1375
PartID:2 Start Year:1995 End year:2001 ModelID:1244
PartID:2 Start Year:1996 End year:2000 ModelID:2361
你知道如何在 SQL While 循环或 SQL 查询中做到这一点。这些是表格:
型号2年
id | model_id | year
1 1 1966
2 2 1973
3 2 1972
4 2 1971
5 2 1970
模型2年2部分
id | model2year_id | part_id
1 9521 1
2 9520 1
3 9519 1
4 8637 2
5 8636 2
这些是示例表:
http://milversite.net/model2year.zip
http://milversite.net/model2year2part.zip
这是上面表的 sql 查询
$result = mysql_query("SELECT * FROM model2year JOIN model2year2part ON model2year2part.model2year_id = model2year.id");
while($row = mysql_fetch_array($result))
{
$part_id = $row['part_id'];
$year = $row['year'];
$model_id = $row['model_id'];
echo "PartID:$part_id Year:$year ModelID:$model_id<br>";
}
但我需要将其插入此结果表中,而不需要重复的 model_id,而不是年份范围为 min。model_id 的最大值:
结果表
part_id | model_id | start_year | end_year
谢谢!