![](/img/trans.png)
[英]Query returns just one record, how to get all records with multiple condition over one column?
[英]How to get multiple records one record using query in mysql
如何能夠從這些數據中查詢:
parking_place | number_of_month | from_date | end_date | monthly_unit_price
A | 3 | 2018-01 | 2018-03 | 3000000
希望顯示結果:
parking_place | month | monthly_unit_price
A | 2018-01 | 3000000
A | 2018-02 | 3000000
A | 2018-03 | 3000000
請建議我如何查詢?
您可以使用日歷表加入:
SELECT
t.parking_place,
t.month,
t.monthly_unit_price
FROM
(
SELECT '2018-01' AS month UNION ALL
SELECT '2018-02' UNION ALL
SELECT '2018-03'
) months
INNER JOIN yourTable t
ON months.month BETWEEN t.from_date AND t.end_date
ORDER BY
months.month;
請注意,最好存儲實際有效的日期文字來表示每個月。 例如,而不是存儲文本'2018-01'
,你可以存儲2018-01-01
的日期文字。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.