Select 从时间字符串中提取小时范围的语句
Select statement to extract range of hours from Time String
我有一个像这样的 PostgreSQL table。
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 之间时,我如何 select ISP 和 city_location
我对分秒毫秒不感兴趣。
我试过这个只处理日期
的查询
select city_location, isp from new_markers where date_='2014-11-24';
但是,我不知道如何为 select9 到 12 小时之间的时间编写 select 语句。
如有任何帮助,我们将不胜感激。谢谢
为此,我做了以下工作:
创建了我的 table:
CREATE TABLE Stuff(city_location VARCHAR(30), isp VARCHAR(50), date_ date, time_ time);
填充了table:
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'
筛选您想要的日期。
我有一个像这样的 PostgreSQL table。
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 之间时,我如何 select ISP 和 city_location
我对分秒毫秒不感兴趣。 我试过这个只处理日期
的查询select city_location, isp from new_markers where date_='2014-11-24';
但是,我不知道如何为 select9 到 12 小时之间的时间编写 select 语句。
如有任何帮助,我们将不胜感激。谢谢
为此,我做了以下工作:
创建了我的 table:
CREATE TABLE Stuff(city_location VARCHAR(30), isp VARCHAR(50), date_ date, time_ time);
填充了table:
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'
筛选您想要的日期。