在1年查询中多次重复每周查询

时间:2015-03-12 14:38:49

标签: mysql

我有一个查询,它给出了1周内'search_term'使用次数的结果。这是以下查询

SELECT search_term, COUNT(DISTINCT log_search.id) AS search_count, COUNT(DISTINCT log_search.user_id) AS unique_search_count 
FROM log_search 
LEFT JOIN log_session_start ON log_session_start.gg = log_search.gg
WHERE log_search.search_term = 'christmas' AND log_search.date_id >= 20140101 AND log_search.date_id <=20140131 AND log_session_start.site_id=1;

正如您所看到的,我使用log_search.date_id来指定一年中的给定周。现在我希望获得给定年份的所有周的结果,例如2014年。我可以在PHP中重复此查询52次,但这不是我想要的。我想要所以我在下一个查询时出现了。

   SELECT  
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20140106 AND log_search.date_id <=20140112 THEN 1 ELSE 0 END), 0 )) AS w1,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20140113 AND log_search.date_id <=20140119 THEN 1 ELSE 0 END), 0 )) AS w2,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20140120 AND log_search.date_id <=20140126 THEN 1 ELSE 0 END), 0 )) AS w3,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20140127 AND log_search.date_id <=20140202 THEN 1 ELSE 0 END), 0 )) AS w4,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20140203 AND log_search.date_id <=20140209 THEN 1 ELSE 0 END), 0 )) AS w5,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20140210 AND log_search.date_id <=20140216 THEN 1 ELSE 0 END), 0 )) AS w6,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20140217 AND log_search.date_id <=20140223 THEN 1 ELSE 0 END), 0 )) AS w7,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20140224 AND log_search.date_id <=20140302 THEN 1 ELSE 0 END), 0 )) AS w8,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20140303 AND log_search.date_id <=20140309 THEN 1 ELSE 0 END), 0 )) AS w9,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20140310 AND log_search.date_id <=20140316 THEN 1 ELSE 0 END), 0 )) AS w10,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20140317 AND log_search.date_id <=20140323 THEN 1 ELSE 0 END), 0 )) AS w11,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20140324 AND log_search.date_id <=20140330 THEN 1 ELSE 0 END), 0 )) AS w12,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20140331 AND log_search.date_id <=20140406 THEN 1 ELSE 0 END), 0 )) AS w13,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20140407 AND log_search.date_id <=20140413 THEN 1 ELSE 0 END), 0 )) AS w14,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20140414 AND log_search.date_id <=20140420 THEN 1 ELSE 0 END), 0 )) AS w15,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20140421 AND log_search.date_id <=20140427 THEN 1 ELSE 0 END), 0 )) AS w16,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20140428 AND log_search.date_id <=20140504 THEN 1 ELSE 0 END), 0 )) AS w17,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20140505 AND log_search.date_id <=20140511 THEN 1 ELSE 0 END), 0 )) AS w18,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20140512 AND log_search.date_id <=20140518 THEN 1 ELSE 0 END), 0 )) AS w19,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20140519 AND log_search.date_id <=20140525 THEN 1 ELSE 0 END), 0 )) AS w20,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20140526 AND log_search.date_id <=20140601 THEN 1 ELSE 0 END), 0 )) AS w21,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20140602 AND log_search.date_id <=20140608 THEN 1 ELSE 0 END), 0 )) AS w22,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20140609 AND log_search.date_id <=20140615 THEN 1 ELSE 0 END), 0 )) AS w23,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20140616 AND log_search.date_id <=20140622 THEN 1 ELSE 0 END), 0 )) AS w24,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20140623 AND log_search.date_id <=20140629 THEN 1 ELSE 0 END), 0 )) AS w25,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20140630 AND log_search.date_id <=20140706 THEN 1 ELSE 0 END), 0 )) AS w26,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20140707 AND log_search.date_id <=20140713 THEN 1 ELSE 0 END), 0 )) AS w27,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20140714 AND log_search.date_id <=20140720 THEN 1 ELSE 0 END), 0 )) AS w28,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20140721 AND log_search.date_id <=20140727 THEN 1 ELSE 0 END), 0 )) AS w29,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20140728 AND log_search.date_id <=20140803 THEN 1 ELSE 0 END), 0 )) AS w30,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20140804 AND log_search.date_id <=20140810 THEN 1 ELSE 0 END), 0 )) AS w31,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20140811 AND log_search.date_id <=20140817 THEN 1 ELSE 0 END), 0 )) AS w32,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20140818 AND log_search.date_id <=20140824 THEN 1 ELSE 0 END), 0 )) AS w33,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20140825 AND log_search.date_id <=20140831 THEN 1 ELSE 0 END), 0 )) AS w34,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20140901 AND log_search.date_id <=20140907 THEN 1 ELSE 0 END), 0 )) AS w35,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20140908 AND log_search.date_id <=20140914 THEN 1 ELSE 0 END), 0 )) AS w36,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20140915 AND log_search.date_id <=20140921 THEN 1 ELSE 0 END), 0 )) AS w37,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20140922 AND log_search.date_id <=20140928 THEN 1 ELSE 0 END), 0 )) AS w38,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20140929 AND log_search.date_id <=20141005 THEN 1 ELSE 0 END), 0 )) AS w39,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20141006 AND log_search.date_id <=20141012 THEN 1 ELSE 0 END), 0 )) AS w40,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20141013 AND log_search.date_id <=20141019 THEN 1 ELSE 0 END), 0 )) AS w41,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20141020 AND log_search.date_id <=20141026 THEN 1 ELSE 0 END), 0 )) AS w42,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20141027 AND log_search.date_id <=20141102 THEN 1 ELSE 0 END), 0 )) AS w43,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20141103 AND log_search.date_id <=20141109 THEN 1 ELSE 0 END), 0 )) AS w44,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20141110 AND log_search.date_id <=20141116 THEN 1 ELSE 0 END), 0 )) AS w45,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20141117 AND log_search.date_id <=20141123 THEN 1 ELSE 0 END), 0 )) AS w46,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20141124 AND log_search.date_id <=20141130 THEN 1 ELSE 0 END), 0 )) AS w47,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20141201 AND log_search.date_id <=20141207 THEN 1 ELSE 0 END), 0 )) AS w48,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20141208 AND log_search.date_id <=20141214 THEN 1 ELSE 0 END), 0 )) AS w49,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20141215 AND log_search.date_id <=20141221 THEN 1 ELSE 0 END), 0 )) AS w50,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20141222 AND log_search.date_id <=20141228 THEN 1 ELSE 0 END), 0 )) AS w51,
COUNT( NULLIF( (CASE WHEN log_search.date_id >= 20141229 AND log_search.date_id <=20150104 THEN 1 ELSE 0 END), 0 )) AS w52
FROM log_search 
LEFT JOIN log_session_start ON log_session_start.gg = log_search.gg
WHERE log_search.search_term = 'christmas' AND log_session_start.site_id=1;

这个查询的问题是它只给我'search_count'而不是'unique_search_count'。

我正在试图弄清楚如何在1次查询中重复第一次查询52次(比如第二次查询)。有人可以帮助我吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

如果您对显示“search_results”而不是“unique_search_results”的第二个查询感到困扰,您可以运行添加了group by的查询。它会显示具有相同搜索结果的分组结果。我完全不了解您的查询,但如果您想要所有unique_search_results的计数,您可以使用group by