我有三个表:
Bike
+----+--------+---------+------+
| id | make | model | year |
+----+--------+---------+------+
| 1 | make-z | model-1 | 2010 |
+----+--------+---------+------+
| 2 | make-z | model-1 | 2011 |
+----+--------+---------+------+
| 3 | make-z | model-1 | 2012 |
+----+--------+---------+------+
| 4 | make-z | model-1 | 2013 |
+----+--------+---------+------+
| 5 | make-z | model-1 | 2014 |
+----+--------+---------+------+
Part
+--------+-------+-------+----------+
| id | name | price | category |
+--------+-------+-------+----------+
| part-A | spg-A | 10.00 | Spring |
+--------+-------+-------+----------+
| part-B | spg-B | 12.00 | Spring |
+--------+-------+-------+----------+
Link
+--------+--------+
| bikeID | partID |
+--------+--------+
| 1 | part-A |
+--------+--------+
| 2 | part-A |
+--------+--------+
| 3 | part-A |
+--------+--------+
| 4 | part-B |
+--------+--------+
| 5 | part-A |
+--------+--------+
我需要建立一个应用图表,以显示哪些零件适合哪辆自行车。
我的问题是:
model-1年2010年-2012年和2014年使用A部分
2013年Model-1年使用B部分
因此,最小值和最大值错过了不包含2013年的事实。
我只需要连续两年获取最小值和最大值即可。
SELECT *, MIN(year) AS syear, MAX(year) AS eyear
FROM bike
LEFT JOIN link ON bike.id = link.bikeID
LEFT JOIN part ON link.partID = part.id
WHERE part.category = 'spring'
GROUP BY make, model, part.id
ORDER BY make, model, year
我得到的输出是:
+---------+--------+------+------+
| model-1 | part-A | 2010 | 2014 |
+---------+--------+------+------+
| model-1 | part-B | 2013 | 2013 |
+---------+--------+------+------+
应该是什么样子
+---------+--------+------+------+
| model-1 | part-A | 2010 | 2012 |
+---------+--------+------+------+
| model-1 | part-B | 2013 | 2013 |
+---------+--------+------+------+
| model-1 | part-A | 2014 | 2014 |
+---------+--------+------+------+
我把它打出来作为我需要做的简单例子。
表中的ID顺序不正确,零件也不规则。
感谢您的帮助