循环遍历3种不同的SQL结果

时间:2019-04-22 19:01:45

标签: javascript sql postgresql loops

我需要遍历3条SQL查询结果。问题在于,它只能带来第一手的结果,而不是全部。

exports.rewardsNotifications = function() {
    var now = new Date()
    var hourUTC = now.getUTCHours()

    if (hourUTC <= 18) {
        db.task(t => {
            return t.any(`SELECT customer.customer_id, customer.phone_number, customer.place_group_id, customer.state, MAX(visit_date_only) AS last_visit, COUNT(visit_date) AS visit_count FROM VISITS
            INNER JOIN customer ON visits.customer_id = customer.customer_id AND visits.place_group_id = customer.place_group_id
            WHERE customer.state = 'on' GROUP BY customer.customer_id`).then(function(visitsData) {

                for(var i=0; i<visitsData.length; i++){
                    var customer_id = visitsData[i]['customer_id']
                    var phone_number = visitsData[i]['phone_number']
                    var place_group_id_visits = visitsData[i]['place_group_id']
                    var last_visit = visitsData[i]['last_visit']
                    var visit_count = visitsData[i]['visit_count']


                return t.any(`SELECT rewards_list.reward_name, rewards_list.required_visits, rewards_list.place_group_id FROM rewards_list`).then(function(rewards){

                    for(var j=0; j<rewards.length; j++){
                        var reward_name = rewards[j]['reward_name']
                        var reward_required_visits = rewards[j]['required_visits']
                        var place_group_id_rewards = rewards[j]['place_group_id']


                    return t.any(`SELECT customer_id, reward_name, place_group_id FROM rewards`).then(function(redeemedRewards){

                        for(var k=0; k<redeemedRewards.length; k++){
                            var redeemed_reward_customer_id = redeemedRewards[k]['customer_id']
                            var reward_redeemed_name = redeemedRewards[k]['reward_name']
                            var reward_rest_group_id = redeemedRewards[k]['place_group_id']


                        console.log('THIS ARE THE REWARDS', reward_name)
                        console.log('THIS IS THE CUSTOMERS', customer_id)

                    }
                    })

                }
                })

                }
            })
        })
    }
}

在我的console.log上,我只是从查询中获得第一个结果(不是所有客户,也不是所有奖励)。 我想做的是(针对所有客户)检查客户兑换的奖励和他们没有兑换的奖励。

0 个答案:

没有答案