久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

node.js - mysql如何通過knex查詢今天和七天內的匯總數據

瀏覽:150日期:2022-06-20 17:05:10

問題描述

具體實現是要在product表中查詢出今天、七天和三十天內的產品數量,具體的sql語句已經寫好了

select sum(inputer as productNum) from `product` where to_days(`createdAt`)= to_days(now());

但是在knex.js里面我這樣寫根本不對

return knex(’product’) .where({ inputer: user, deletedAt: null }) .andWhere(’to_days(add_time)’, ’=’, ’to_days(now())’) .sum(’inputer as productNum’) .then(function (productRow) { return { product: productRow }; })

用having也不對,knex文檔里沒有看到聚合函數的使用方法,求指教

return knex(’product’) .where({ inputer: user, deletedAt: null }) .groupBy(id) .having(’to_days(add_time)’, ’=’, ’to_days(now())’) .sum(’inputer as productNum’) .then(function (productRow) { return { product: productRow }; })

問題解答

回答1:

沒用過knex.js,但SQL好像復雜化了(原SQL會對createdAt字段進行運算,有可能會讓該字段的索引失效)。

SELECT sum(inputer) AS product_num FROM `product`WHERE createdAt >= ?

通過程序計算出今天、七天前和三十天前的起始時間(即yyyy-MM-dd 00:00:00),然后代入SQL即可。

相關文章:
主站蜘蛛池模板: 潮安县| 吴堡县| 昆山市| 南投县| 寿阳县| 西青区| 鲁甸县| 文昌市| 安徽省| 六枝特区| 靖远县| 沐川县| 侯马市| 云梦县| 出国| 辉县市| 都江堰市| 穆棱市| 五莲县| 灵台县| 科尔| 昌平区| 永修县| 固始县| 竹山县| 茶陵县| 吉安市| 慈溪市| 文安县| 石河子市| 科技| 涪陵区| 连州市| 项城市| 巨鹿县| 盐边县| 关岭| 石门县| 灵台县| 漠河县| 永川市|