我正在尝试将唯一的术语计为总计数。
这是原始查询,工作正常
->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.
因此数据表可以对行进行计数并将其用作分页参数。
但是当它删除了select时,它会得到所有的行,因为DISTINCT不再存在了。
我被剥夺了睡眠,所以我总是坚持尝试和错误永远。请帮助lol:P
答案 0 :(得分:0)
排序
->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下计算UNIQUE n_ocurrences,因为n_ocurrences只返回整体计数。