简体   繁体   中英

query with INNER JOIN

The tables are created and have some data inserted I am trying to get the route from the arrivaltimes table with the aid of JOIN INNER but I am getting nothing back I have already tried to adjust the time to the current time tin th arrivaltimes table but the result is always empty. I am not getting any error. Is something wrong with my query?

    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)

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


SELECT route from arrivaltimes INNER JOIN stops ON  arrivaltimes.stop_id=stops.stop_id where arrivaltime = now()

在此处输入图片说明

arrivaltime is having datatype time and its in the format H:i:s .

In the where clause you are using now() and it will be in the format Ymd H:i:s

You need to use curtime function for that.

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2015-05-10 23:46:44 |
+---------------------+
1 row in set (0.00 sec)

mysql> select curtime();
+-----------+
| curtime() |
+-----------+
| 23:46:53  |
+-----------+
1 row in set (0.00 sec)


where arrivaltime = curtime()

your query is perfectly fine the problem is only with the time you provide as input.

The format of TIME data type is 'HH:MM:SS' so use time(now()) this will give you proper results.

mysql> select time(now());

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