gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 22:47:55 25 4
gpt4 key购买 nike

我有一个查询,它给出了“search_term”在 1 周内使用了多少次的结果。这是以下查询

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 次(如第二个查询)。有人可以帮我吗?

谢谢!

最佳答案

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

关于mysql - 在 1 年查询中多次重复每周查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29012692/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com