码迷,mamicode.com
首页 > 数据库 > 详细

常用SQL记录

时间:2016-12-14 22:30:20      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:mysql

1、设定变量,表内连接


SET @baidu_date = 20161213;



SET @this_30_start = 20161016;



SET @this_30_stop = 20161114;



SET @last_180_start = 20160519;



SET @last_180_stop = 20161114;


SELECT

a.*, b.30_overall_QA,

b.30_mobile_QA

FROM

(

SELECT

id,

search_term,

create_time,

TRUNCATE (AVG(overall_trend), 0) 180_overall_QA,

TRUNCATE (AVG(pc_trend), 0) 180_pc_QA,

TRUNCATE (AVG(mobile_trend), 0) 180_mobile_QA

FROM

baidu_index_daily

WHERE

create_time = @baidu_date

AND index_date BETWEEN @last_180_start

AND @last_180_stop

GROUP BY

id

) a,

(

SELECT

id,

TRUNCATE (AVG(overall_trend), 0) 30_overall_QA,

TRUNCATE (AVG(mobile_trend), 0) 30_mobile_QA

FROM

baidu_index_daily

WHERE

create_time = @baidu_date

AND index_date BETWEEN @this_30_start

AND @this_30_stop

GROUP BY

id

) b

WHERE

a.id = b.id;



















本文出自 “小小水滴” 博客,请务必保留此出处http://wangzan18.blog.51cto.com/8021085/1882706

常用SQL记录

标签:mysql

原文地址:http://wangzan18.blog.51cto.com/8021085/1882706

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!