在MySQL中表示票价图表

我正在为巴士运输系统订票系统。 在MySQL中,我无法将票价表作为表格。 下面给出了作为电子表格文件给我的一个特定路线的票价表的一个例子。 请让我知道这可以表示为一个MySQL表。

请注意,这只是指定路线之一的一部分。 实际路线有40多个地点,有30多条路线。 所以,我希望以某种方式将这张excel表导出为SQL。

location1 | | | | 0.100 | location2 | | | 0.200 | 0.200 |location3 | | 0.300 | 0.300 |0.200 |location4 | 0.500 | 0.500 |0.400 |0.300 |location5 | 0.500 | 0.500 |0.400 |0.300 |0.200 |location6 

这里从位置1到位置3的票价为0.200,从位置3到位置6的票价为0.400。

请让我知道如何查询给定源和目标forms的MySQL表一旦实施。

就像:

 RATES location_from location_to price 

并抬头:

 SELECT price FROM rates WHERE location_from=3 AND location_to=4 

将返回:0.400

总是input最低的那个location_form,所以不要添加4,3。然后你得到双重logging。

但根据您的需要,您还可以使用距离并计算它们。 完全取决于你的业务需求。

我设法在Excel中使用下面的VBmacros将它转换成一个表,其中一些文本处理转换成一个MySQL查询插入到数据库

 Sub ReversePivotTable() ' Before running this, make sure you have a summary table with column headers. ' The output table will have three columns. Dim SummaryTable As Range, OutputRange As Range Dim OutRow As Long Dim r As Long, c As Long On Error Resume Next Set SummaryTable = ActiveCell.CurrentRegion If SummaryTable.Count = 1 Or SummaryTable.Rows.Count < 3 Then MsgBox "Select a cell within the summary table.", vbCritical Exit Sub End If SummaryTable.Select Set OutputRange = Application.InputBox(prompt:="Select a cell for the 3-column output", Type:=8) ' Convert the range OutRow = 2 Application.ScreenUpdating = False OutputRange.Range("A1:C3") = Array("To", "From", "Fare") For r = 2 To SummaryTable.Rows.Count For c = 2 To SummaryTable.Columns.Count If SummaryTable.Cells(r, c) <> Empty And IsNumeric(SummaryTable.Cells(r, c)) Then OutputRange.Cells(OutRow, 1) = SummaryTable.Cells(r, r) OutputRange.Cells(OutRow, 2) = SummaryTable.Cells(c, c) OutputRange.Cells(OutRow, 3) = SummaryTable.Cells(r, c) OutputRange.Cells(OutRow, 3).NumberFormat = SummaryTable.Cells(r, c).NumberFormat OutRow = OutRow + 1 End If Next c Next r End Sub 

这会给我3列,说明location_from,location_to和价格描述为@ luc-franken。 我将其保存为一个CSV文件,并用一些正则expression式replace设法将其转换为MySQL查询。