![41cf6e6fc201b751780af6f5085a0c78.png](https://img-blog.csdnimg.cn/img_convert/41cf6e6fc201b751780af6f5085a0c78.png)
一 MongoDB 语法
- 查询去重用户量
db.getCollection('tablename').distinct("user_id").length
mongo:db.getCollection('user_app_action_display_20190314').distinct("user_id").length
MySQL:select count(distinct user_id from user_app_action_display_20190314
- LIKE语句的使用方法
mongo:
db.getCollection('tablename').
find({"event_object.eventCategory":{$regex:/首页_display/}}) #求曝光量数据
MySQL:
select * from tablename where 字段名 like '%首页_display%'
注意:event_object在mongo中以json字符串的形式存在存储,eventCategory是json中的一个字段,在这里普及一下json字符串:json 就是一个根据某种约定格式编写的纯字符串,不具备任何数据结构的特征。 其形态类似python中字典表现规则,但字典本身是一个完整的数据结构,实现了一切自身该有的算法,而json只是字符串
- 聚合查询1
mongo:
db.user_app_action_20190128.aggregate( [
{ "$match": { "event_object.eventCategory": "首页_click" } },
{
"$group": {
"_id": "$grade",
"count": {"$sum": 1}
}
}
] )
MySQL:
select
grade,count(1)
from tablename
where event_object.eventCategory="首页_click"
group by grade
二 HIVE SQL
- 查看分区信息
show partitions table_name;
- 根据分区查询数据
select table_coulm from table_name where partition_name = '20190317'
- hive时间控制常用是转成时间戳再加减
/*将create_at往后加8个小时:常用于时区转化*/
SELECT
from_unixtime(unix_timestamp(create_at)+28800,'yyyy-MM-dd') as date,
count(DISTINCT user_id) as cnt
FROM dw_fac_user_app_action_display
GROUP BY from_unixtime(unix_timestamp(create_at)+28800,'yyyy-MM-dd')
/*两个时间函数不可直接相减,同样要转化为秒再相减*/
(unix_timestamp(time1)-unix_timestamp(time2))/24/3600 ---求time1与time2相差的天数
- 计算时间差
select datediff('2019-03-20','2019-03-05') #15 单位为day
* 在MySQL中可是可以执行的
- 获取当前时间的方式
case1:select date_format(current_timestamp,'yyyy-MM-dd')
case2:select from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:mm:ss')
* MySQL中用curdate() & now()函数
- 获取几天前、几小时前的时间
select from_unixtime(unix_timestamp(current_timestamp)-86400,"yyyy-mm-dd")
- 正则匹配
grade REGEXP '[^0-9.]')=1; //查询grade列不是数字的数据
grade REGEXP '[^0-9.]')=0; //查询grade列是数字的数据
- CAST()函数
把一个字段转成另一个字段,比起orcale,MySQL相比之下就简单得多了,只需要一个cast()函数就能搞定。其语法为:(字段名 as 类型 ),其中类型可以为:
CHAR[(N)] 字符型
DATE 日期型
DATETIME 日期和时间型
DECIMAL float型
SIGNED int
TIME 时间型