仅计算基于内部 SELECT JOIN 的唯一术语

发布于 2024-12-02 10:24:36 字数 1070 浏览 1 评论 0原文

我试图仅将唯一术语计为总计数。

这是原始查询,它工作正常

->select('DISTINCT search_tags.term AS t_name, nbr', FALSE)        
    ->from('search_tags LEFT JOIN (SELECT term AS tk, COUNT(search_tags.term) AS nbr FROM search_tags GROUP BY search_tags.term) AS TR ON search_tags.term = TR.tk ')
    ->where('search_tags.dt_added >=', '2011-08-01 09:48:54') 
    ->where('search_tags.dt_added <=', '2011-09-02 09:48:54');

// returns: [twitter,12],[facebook,6].....

问题是此代码运行数据表(datatable.net),因此数据表删除选择行并将其更改为:

SELECT COUNT(*) AS numrows
FROM (search_tags LEFT JOIN (SELECT term AS tk, COUNT(search_tags.term) AS nbr FROM search_tags     GROUP BY search_tags.term) AS TR ON search_tags.term = TR.tk)
WHERE `search_tags`.`dt_added` >= '2011-08-01 09:48:54'
AND `search_tags`.`dt_added` <= '2011-09-02 09:48:54'

// returns the same  [twitter,12],[facebook,6]..... BUT the pagination is broken.

因此数据表可以计算行数并将其用作分页参数。

但是当它删除选择时,它会获取所有行,因为 DISTINCT 不再存在。

我睡眠不足,所以我就像永远陷入尝试和错误中。请帮忙哈哈:P

I'm trying to count only unique terms as a TOTAL count.

This is the original query and it works fine

->select('DISTINCT search_tags.term AS t_name, nbr', FALSE)        
    ->from('search_tags LEFT JOIN (SELECT term AS tk, COUNT(search_tags.term) AS nbr FROM search_tags GROUP BY search_tags.term) AS TR ON search_tags.term = TR.tk ')
    ->where('search_tags.dt_added >=', '2011-08-01 09:48:54') 
    ->where('search_tags.dt_added <=', '2011-09-02 09:48:54');

// returns: [twitter,12],[facebook,6].....

The thing is that this code runs a datatable (datatable.net) so the datatable removes the select line and change it to:

SELECT COUNT(*) AS numrows
FROM (search_tags LEFT JOIN (SELECT term AS tk, COUNT(search_tags.term) AS nbr FROM search_tags     GROUP BY search_tags.term) AS TR ON search_tags.term = TR.tk)
WHERE `search_tags`.`dt_added` >= '2011-08-01 09:48:54'
AND `search_tags`.`dt_added` <= '2011-09-02 09:48:54'

// returns the same  [twitter,12],[facebook,6]..... BUT the pagination is broken.

So the datatable can count the rows and use it as a pagination param.

But when it removes the select, it get all the rows as the DISTINCT is not there anymore.

I'm sleep deprived so I'm like stuck on try and error forever. Please help lol :P

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆离笙 2024-12-09 10:24:36

已排序

->select('DISTINCT st5.term AS t_name, n_ocurrences', FALSE)        
    ->from("search_tags AS st4 
    RIGHT OUTER JOIN (SELECT DISTINCT st.term, n_ocurrences  
    FROM search_tags AS st 
    JOIN 
    (SELECT term AS n_term, COUNT(term) AS n_ocurrences 
    FROM search_tags AS st2 
    GROUP BY st2.term)
    AS st3 ON st3.n_term = st.term  
    WHERE st.dt_added >= '$min' 
    AND st.dt_added <= '$max')  AS st5 ON st5.term = 1");  

必须添加一些初始选择才能检索正确的数量。
现在我需要计算 $min、$max 下的唯一 n_ocurrences,因为 n_ocurrences 仅返回总数。

Sorted out

->select('DISTINCT st5.term AS t_name, n_ocurrences', FALSE)        
    ->from("search_tags AS st4 
    RIGHT OUTER JOIN (SELECT DISTINCT st.term, n_ocurrences  
    FROM search_tags AS st 
    JOIN 
    (SELECT term AS n_term, COUNT(term) AS n_ocurrences 
    FROM search_tags AS st2 
    GROUP BY st2.term)
    AS st3 ON st3.n_term = st.term  
    WHERE st.dt_added >= '$min' 
    AND st.dt_added <= '$max')  AS st5 ON st5.term = 1");  

Had to add some inception selects to retrieve the right amount.
Now I need to count the UNIQUE n_ocurrences under the $min, $max as the n_ocurrences is returning only the overall count.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文