SQL查询时间段方法

今天遇到一个查询时间段的需求,查阅了好长时间,在此汇总下,以免下次再忘记了:
PostgreSQL:
需要使用到to_Char()函数,在这里记下。

SELECT *
FROM (SELECT to_char(transationtime, 'HH24:MI:SS') as times, *
      FROM zhifubaobalance) timeBalance
where (timeBalance.times between '21:00:00' and '23:59:59'
    or timeBalance.times between '00:00:00' and '06:59:59')
  and (transationparenter not like '%基金%')
  and money > 500
order by transationtime desc;

这里查询的是晚上9点至次日7点之间的交易数据。
本表结构如下:

CREATE TABLE cloud04.zhifubaobalance (
	transactionnumber varchar(36) NOT NULL,
	sellerbillnumber varchar(36) NULL,
	transationtime timestamp NULL,
	paytime timestamp NULL,
	lastchangeon timestamp NULL,
	transationresource varchar(36) NULL,
	"type" varchar(36) NULL,
	transationparenter varchar(100) NULL,
	goodsname varchar(100) NULL,
	"money" numeric(6,2) NULL,
	incomeorexpenses varchar(10) NULL,
	transactionstate varchar(10) NULL,
	servicecharge numeric(6,2) NULL,
	refund numeric(6,2) NULL,
	moneystate varchar(10) NULL
);

你可能感兴趣的:(sql)