获取当前日期时间:select now()
获取当前日期:select today()
获取昨日日期:select yesterday()日期时间转日期:select toDate(‘2021-05-11 14:31:31’)
日期转时间戳:select toUnixTimestamp(toDateTime(‘2021-05-01’))
时间戳转日期时间:select toDateTime(1620714857)
参考文档:https://www.saoniuhuo.com/article/detail-1222.html
日期类型转换
select
toDateTime('2022-05-20 05:20:05') -- 字符转日期
,formatDateTime(toDateTime('2022-05-20 05:20:05'),'%Y-%m-%d %H:%M:%S') -- 日期转字符并格式化
,parseDateTimeBestEffort('2022-05-20 05:20:05') -- 将字符串型的日期转为时间类型
;
时区
SELECT
toDateTime('2022-05-20 00:00:00', 'UTC') AS time_utc,
toTypeName(time_utc) AS type_utc,
toInt32(time_utc) AS int32utc,
toTimeZone(time_utc, 'Asia/Shanghai') AS time_sahnghai, -- 8小时
toTimeZone(time_utc, 'Asia/Yekaterinburg') AS time_yekat, -- 6小时
toTypeName(time_yekat) AS type_yekat,
toInt32(time_yekat) AS int32yekat,
toTimeZone(time_utc, 'US/Samoa') AS time_samoa,
toTypeName(time_samoa) AS type_samoa,
toInt32(time_samoa) AS int32samoa
FORMAT Vertical;
数据备份
-- 这里只是生成表结构
create table table_name2 as table_name1 ;
-- 查询数据插入目标表
insert into table_name2
select * from table_name1 ;
数据同步
-- 数据同步必须有一个主键,如果没有需要自己生成
create table table_name1 engine = MergeTree order by id
as select rowNumberInAllBlocks() id , * from
mysql('IP','库名' --服务器地址+库名
,'table_name' --需要同步的数据表
,'acct','password'); --账号密码
进程
查询进程
show processlist ;
杀掉进程:
kill query where query_id='70442d9b-7fc5-4a0e-81be-9543431a4882';
使用工具查询设置时区(DataGrip 2021.1.3)
配置->Advanced
- use_server_time_zone 设置为=> false
- use_time_zone 设置为=> Shanghai
- clickhouse-jdbc的0.3.1版本时间返回时区有bug,在General->driver中选择clickhouse-jdbc的版本为0.2.4,或等待官方修复bug后使用新版本
保存重新建立连接查询,此时时区已更改