请帮我把以下sql语句用strapi写出来select a from quota_records a left join quota_records_user_links b on aid = bquota_record_id where date_truncdayacreated_atdate = $nowTime and asource =
在Strapi中,可以使用QueryBuilder来构建SQL查询语句。根据给定的SQL语句,以下是使用Strapi构建的等效查询:
const nowTime = new Date(); // 当前时间
const data = {
user_id: 123 // 用户ID
};
const query = strapi.query('quota-records')
.model.query(qb => {
qb.select('a.*')
.from('quota_records as a')
.leftJoin('quota_records_user_links as b', 'a.id', 'b.quota_record_id')
.whereRaw(`date_trunc('day', a.created_at)::date = '${nowTime}'`)
.andWhere('a.source', 'share')
.andWhere('b.user_id', data.user_id)
.orderBy('a.created_at');
})
.fetchAll();
console.log(query); // 执行查询
请注意,${nowTime}和${data.user_id}是变量,你需要根据实际情况进行替换。此外,还需要根据你的Strapi配置和数据模型进行适当的调整
AI 优化版
原文地址:https://www.cveoy.top/t/topic/hJ0l 著作权归作者所有。请勿转载和采集!