简体   繁体   中英

Query the foreign keys in the two tables

How can I query the route in the routes table in my database depends on the lat, longi of stops table and the arrivaltime in arrivaltimes table? So if the lat and longi in the stops table are found I want first to check whether there is arrivaltime (here the current time must be equal to the arrivaltime ) of this stop_id in arrivaltimes table then depend on it I want to retrieve all route from the routes table for this stop_id ?

 CREATE TABLE IF NOT EXISTS stops
    (stop_id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, 
     name varchar(30) NOT NULL, 
     lat double(10,6) NOT NULL, 
     longi double(10,6)NOT NULL) 

  INSERT INTO stops(name,  lat, longi) values
      ('ABC', '63.838039', '18.700440' ),
      ('DEF', '63.840642', '18.701246' ),             
      ('HIG', '63.868863', '18.665438' )

   CREATE TABLE IF NOT EXISTS arrivaltimes(arrivaltimes_id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
      weekday VARCHAR(20) NOT NULL,
      arrivaltime time NOT NULL,
      stop_id INT, FOREIGN KEY fk_stop_id(stop_id) REFERENCES stops(stop_id) )

 INSERT INTO arrivaltimes(stop_id, arrivaltime, weekday) values
       ('1', 'mon-fri', '05:30' ),
       ('1', 'mon-fri', '06:07' )

  CREATE TABLE IF NOT EXISTS routes
    (routes_id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
     route INT(11) NOT NULL, 
     stop_id INT, FOREIGN KEY fk_stop_id(stop_id) REFERENCES stops(stop_id) )

  INSERT INTO routes(route) values
     ('1', '1'),
     ('1', '9')

The JDBC code:

        Calendar now = Calendar.getInstance();
        int day = now.get(Calendar.DAY_OF_WEEK);
        int hour = now.get(Calendar.HOUR_OF_DAY);
        int minute = now.get(Calendar.MINUTE);
        String time =  hour+":"+minute;

        Statement stt = con.createStatement();

        PreparedStatement preparedLatLong = con
                .prepareStatement("SELECT lat, longi from stops");
        ResultSet rsLatLong = preparedLatLong.executeQuery();
        while (rsLatLong.next()) {
            double lat_stop = rsLatLong.getDouble("lat");
            double lon_stop = rsLatLong.getDouble("longi");
            double distStops = haversineDistance(latD, longD, lat_stop, lon_stop);
            if(distStops <= 10){
// Here how can I query the arrivaltime and the route?
                PreparedStatement preparedTime = con.prepareStatement("SELECT arrivaltime from arrivaltimes WHERE arrivaltime=time"
                        + "values(?)");
                ResultSet rsArrivaletime = preparedTime.executeQuery();
                while(rsArrivaletime.next()){

                }
            }   
        }

在此处输入图片说明

If the first record in the stops table is the location then I want to check the arrivaltime in the arrivaltimes table if the current time = 13:30 then I want to get the 1 and 9 route number from the routes table. I hop it is more clear now.

You can use joins with a where clause load everything together.

SELECT a.route FROM routes a 
LEFT JOIN arrivaltimes b ON a.stop_id = b.stop_id
LEFT JOIN stops c ON a.stop_id = c.stop_id 
WHERE b.arrivaltime = ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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