我有一个像这样的PostgreSQL表。
city_location | isp | date_ | time_
---------------+------------------------+------------+--------------
Cambridge | Akamai Technologies | 2014-11-23 | 12:59:37.112
Mountain View | Level 3 Communications | 2014-11-24 | 12:59:37.112
Cambridge | Akamai Technologies | 2014-11-24 | 9:59:37.112
Mountain View | Level 3 Communications | 2014-11-25 | 9:59:37.112
当date_为2014-11-24且时间介于9到12之间时,如何选择ISP和city_location。
我对分钟,秒和毫秒不感兴趣。 我尝试过这个只处理日期的查询
select city_location, isp from new_markers where date_='2014-11-24';
但是,我不知道如何编写select语句来选择9到12小时之间的时间。
任何有关此的帮助将不胜感激。谢谢
答案 0 :(得分:2)
为实现这一目标,我做了以下工作:
创建我的表:
CREATE TABLE Stuff(city_location VARCHAR(30), isp VARCHAR(50), date_ date, time_ time);
填充表格:
INSERT INTO Stuff VALUES('Cambridge', 'Akamai Technologies', '2014-11-23', '12:59:37.112');
INSERT INTO Stuff VALUES('Mountain View', 'Level 3 Communications', '2014-11-24', '12:59:37.112');
INSERT INTO Stuff VALUES('Cambridge', 'Akamai Technologies', '2014-11-24', '12:59:37.112');
INSERT INTO Stuff VALUES('Mountain View', 'Level 3 Communications', '2014-11-25', '12:59:37.112');
INSERT INTO Stuff VALUES('Cambridge', 'Akamai Technologies', '2014-11-26', '10:59:37.112');
INSERT INTO Stuff VALUES('Mountain View', 'Level 3 Communications', '2014-11-26', '10:59:37.112');
然后运行以下查询:
SELECT * FROM Stuff WHERE time_ BETWEEN '09:00:00' AND '12:00:00';
或这些 - 相同的结果:
SELECT * FROM Stuff WHERE time_ BETWEEN '09:00' AND '12:00';
SELECT * FROM Stuff WHERE time_ >= '09:00' AND time_ <= '12:00';
就个人而言,我更喜欢BETWEEN
语法,但这更像是personal preference。
结果:
city_location isp date_ time_
------------- --- ----- ------
Cambridge; Akamai Technologies; 2014-11-26; 10:59:37.112
Mountain View; Level 3 Communications; 2014-11-26; 10:59:37.112
如您所见,查询会选出9到12之间的记录。只需添加
即可AND date_ = 'year-mon-day'
过滤所需的日期。