MySQL查询获取旅行路线

时间:2014-04-04 11:50:29

标签: php mysql


我有下表:

+--------+----------+---------+---------+---------
|  From  |    To    |Departure| Arrival |   ID   | 
+--------+----------+---------+---------+---------
|   A    |    B     |   0900  |   0930  |   1    | 
+--------+----------+---------+---------+---------
|   C    |    D     |   1000  |   1030  |   2    | 
+--------+----------+---------+---------+---------
|   B    |    C     |   1100  |   1130  |   3    | 
+--------+----------+---------+---------+---------
|   D    |    E     |   1200  |   1230  |   4    | 
+--------+----------+---------+---------+---------
|   C    |    D     |   1300  |   1330  |   5    | 
+--------+----------+---------+---------+---------


  • 出发/到达时间和 ID 始终在提升;
  • C_D 可以在 B_C 之前和之后找到。

我希望从 A 转到 D ,因此旅行路线应为 ID1 ID3 ID5 A_B B_C C_D

感谢任何帮助。
感谢。

3 个答案:

答案 0 :(得分:1)

您可以在存储过程中解决此问题。但是这种算法在编程语言在内存中执行时可能会更快。只需确保已加载完整的数据集,因此您不必每次迭代都执行查询。

伪代码:

to = 'D'
prev_to = 'A'
array = array();
while (prev_to != 'D') {
  select arrival, to into prev_arrival, prev_to
  from table 
  where departure > prev_arrival 
  and from = prev_to;

  array[] = [arrival => prev_arrival, to => prev_to]
}

return array

编辑:我想我没有更好的事情可做;)

此类将在给定的开始和结束时间之间搜索从A到D的所有路径。就像一个公共交通应用程序。您可能希望使用自己的数据库连接方法。 (只是不要再使用mysql_ *函数)

<?php

class RoutePlanner
{
    /** @var string */
    protected $departureTime;
    /** @var string */
    protected $departureLocation;
    /** @var string */
    protected $arrivalTime;
    /** @var string */
    protected $arrivalLocation;
    /** @var array */
    protected $schedule;
    /** @var mysqli */
    protected $db;

    /**
     * @param string $departureTime
     * @param string $departureLocation
     * @param string $arrivalTime
     * @param string $arrivalLocation
     * @throws InvalidArgumentException
     */
    public function __construct($departureTime, $departureLocation, $arrivalTime, $arrivalLocation)
    {
        $this->departureTime = $departureTime;
        $this->departureLocation = $departureLocation;
        $this->arrivalTime = $arrivalTime;
        $this->arrivalLocation = $arrivalLocation;
    }

    /**
     * @return array
     */
    public function getRoutes()
    {
        $schedule = $this->fetchSchedule();
        $routes = $this->searchRoutes($schedule);
        return $routes;
    }

    /**
     * Search all routes that start and end between given times
     * @param array $schedule - passing as parameter to ensure the order of execution
     * @return array
     */
    protected function searchRoutes(array $schedule)
    {
        $routes = array();

        foreach ($schedule as $i => $row)
        {
            if ($row['from'] == $this->departureLocation)
            {
                $routes[] = $this->getRoute($schedule, $i);
            }
        }

        return $routes;
    }

    /**
     * Get the route when starting at given point and time
     * @param $schedule
     * @param $start
     * @return array
     */
    protected function getRoute($schedule, $start)
    {
        $steps = array();

        $from = $this->departureLocation;
        $time = $this->departureTime;

        for ($i = $start; $i < count($schedule); $i++)
        {
            $row = $schedule[$i];
            if ($row['from'] == $from && $row['departure'] > $time)
            {
                $steps[] = $row;
                $from = $row['to'];
                $time = $row['arrival'];
            }
        }

        return $steps;
    }

    /**
     * @return array
     */
    protected function fetchSchedule()
    {
        if (! empty($this->schedule))
            return $this->schedule;

        $sql = "select * from schedule where departure >= ? and arrival <= ?";

        $db = $this->getDatabase();
        $statement = $db->prepare($sql);
        $statement->bind_param("ss", $this->departureTime, $this->arrivalTime);
        $statement->execute();
        $result = $statement->get_result();

        $this->schedule = $result->fetch_all(MYSQLI_ASSOC);

        $statement->close();
        $result->free();

        return $this->schedule;
    }

    /**
     * @return mysqli
     */
    protected function getDatabase()
    {
        if (empty($this->db))
            $this->db = new mysqli('localhost', 'user', 'pass', 'database');

        return $this->db;
    }

    public function __destroy()
    {
        if (! empty($this->db))
            $this->db->close();
    }
}

使用类似:

<?php

$planner = new RoutePlanner('Amsterdam', '0300', 'Berlin', '1030');
$routes = $planner->getRoutes();

答案 1 :(得分:1)

你可以自己交叉加入桌子以获得每一步,然后你可以在你的PHP中运行一个循环或任何检查出发和到达时间的循环,以确保离开是在上一次旅行到来之后:

所以,你需要某种条件逻辑,但这是SQL背后的基本思想:

SELECT a.id,b.id,a.From, a.To, a.Departure,a.Arrival,
b.From,b.To,b.Departure,b.Arrival
FROM travel a
JOIN travel b
ON a.To = b.From
WHERE a.From = 'A'
OR b.To = 'D'
ORDER BY a.Arrival,a.Departure ASC,
b.Arrival,b.Departure ASC;

如果你想一举完成整个事情,你可以第三次加入表格,然后使用WHERE a.From = 'A' AND c.To = 'D',但我认为只对自己进行一次连接会更有效率。然后在循环中使用条件逻辑和WHERE子句来计算行程。

SQLFiddle

答案 2 :(得分:0)

这不可能使用MySQL查询。但是,您可以使用您的编程语言(如php,asp(web),c#(windows)等)来实现此目的。