简体   繁体   English

如何在mysql中使用查询获取一条记录的多条记录

[英]How to get multiple records one record using query in mysql

How to be able to query from this data:如何能够从这些数据中查询:

parking_place | number_of_month  | from_date | end_date  | monthly_unit_price
A             | 3                | 2018-01   | 2018-03   | 3000000

Desire to show results:希望显示结果:

parking_place | month   | monthly_unit_price
A             | 2018-01 | 3000000
A             | 2018-02 | 3000000
A             | 2018-03 | 3000000

please suggest me how to query?请建议我如何查询?

You may join using a calendar table:您可以使用日历表加入:

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;

Note that it would be better to store actual valid date literals to represent each month.请注意,最好存储实际有效的日期文字来表示每个月。 For example, instead of storing the text '2018-01' , you could store 2018-01-01 as a date literal.例如,而不是存储文本'2018-01' ,你可以存储2018-01-01的日期文字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM