首页 > 数据库 > > MySQL、MongoDB、hive查询语法笔记
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_20190314mongo:
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只是字符串
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 gradeshow partitions table_name;select table_coulm from table_name where partition_name = '20190317'/*将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.]')=; //查询grade列是数字的数据把一个字段转成另一个字段,比起orcale,MySQL相比之下就简单得多了,只需要一个cast()函数就能搞定。其语法为:(字段名 as 类型 ),其中类型可以为:
CHAR[(N)] 字符型
DATE 日期型
DATETIME 日期和时间型
DECIMAL float型
SIGNED int
TIME 时间型
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69946831/viewspace-3125023/ ,如需转载,请注明出处,否则将追究法律责任。