如何在node.js中删除knex.raw中的查询

时间:2018-06-05 12:14:09

标签: mysql node.js knex.js bookshelf.js

我在mysql中完成了这个查询,但我不知道如何在knex.raw中执行此操作。

    select ((select leave_trackers.available_leaves from leave_trackers) -
(SELECT ((DATEDIFF('2018-06-11', '2018-06-01') + 1) - 
(WEEK('2018-06-11') - WEEK('2018-06-01')) -
(case when weekday('2018-06-11') = 6 then 1 else 0 end) -
(SELECT IFNULL(sum(total), 0)
from
(
select count(holidays.date) as total
FROM holidays, leave_applications 
WHERE holidays.date between '2018-06-01' and '2018-06-01'
GROUP BY holiday_id) as holiday_leave 
)
) as available_leaves
FROM leaves
group by leave_id
));  

有人可以帮我转换成knex.raw吗?

2 个答案:

答案 0 :(得分:1)

knex.raw(`
select ((select leave_trackers.available_leaves from leave_trackers) -
(SELECT ((DATEDIFF('2018-06-11', '2018-06-01') + 1) - 
(WEEK('2018-06-11') - WEEK('2018-06-01')) -
(case when weekday('2018-06-11') = 6 then 1 else 0 end) -
(SELECT IFNULL(sum(total), 0)
from
(
select count(holidays.date) as total
FROM holidays, leave_applications 
WHERE holidays.date between '2018-06-01' and '2018-06-01'
GROUP BY holiday_id) as holiday_leave 
)
) as available_leaves
FROM leaves
group by leave_id
))
`).then(res => console.log(res));

如果这不起作用,我们需要更多关于出错的信息。

答案 1 :(得分:0)

我找到了解决方法:-),

knex.raw('((select leave_trackers.available_leave_days from leave_trackers) - (select ((DATEDIFF(?, ?) + 1) - (WEEK(?) - WEEK(?)) - (case when weekday(?) = 6 then 1 else 0 end) - (SELECT IFNULL(sum(total), 0) from (select count(holidays.date) as total FROM holidays WHERE holidays.date between ? and ? && holidays.location_id = ?) as holiday_leave )) AS total_available_days)) AS total_available_leave_days',
                                        ['2018-06-11', '2018-06-01', '2018-06-11', '2018-06-01', '2018-06-11', '2018-06-01', '2018-06-11', '1']
                                    )