NodeJS My SQL在一个会话中运行多个查询

时间:2017-08-22 10:12:06

标签: javascript mysql node.js asynchronous

我的数据库中有树关系表。 类别 房间 reservation_detail

首先,我想用它的过滤器选择类别表。

其次我想选择第一次查询返回的房间表传递参数。

第三,我想选择第二个查询返回的reservation_detail表传递参数。

SELECT id,name,price,currency FROM category  where branch_id=84;

SELECT id,room_no,name,price,currency FROM room  where branch_id=84 and category_id=64;

SELECT d.id,
       d.create_date,
       d.update_date,
       d.room_id,
       d.status_id,
       d.start_date,
       d.end_date, 
       s.name as status_name,
       a.id as reservation_id,
       a.person_no as person_no,
       p.first_name,
       p.last_name,
       p.email 
FROM reservation_detail d 
inner join reservation_status s on d.status_id=s.id 
inner join reservation a on d.reservation_id=a.id 
inner join person p on a.person_no=p.personal_no 
where d.room_id=44 
  and d.start_date >'2017-07-11'
  and d.start_date<'2017-09-20';

一个类别包含多个房间,所以我想在循环中运行第二个查询,这里我有异步问题,如何解决这个问题?

这是节点js代码

getCategory = function (branch_id, callback) {
    var categoryData;
    var query = 'SELECT id,name,price,currency FROM category  where branch_id=?';
    pool.getConnection(function (err, connection) {
        connection.query(query, [branch_id], function (error, row, fields) {
            if (error) {
                callback(error, null);
            } else {
                categoryData = row;
                callback(null, categoryData);
            }
            connection.release();
        });
    });
}

getRoom = function (branch_id, categoryID, callback) {
    var roomData;
    var roomSql = 'SELECT id,room_no,name,price,currency FROM room  where branch_id=? and category_id=?';
    pool.getConnection(function (err, connection) {
        connection.query(roomSql, [branch_id, categoryID], function (error, row, fields) {
            if (err) {
                callback(err, null);
            } else {
                roomData = row;
                callback(null, roomData);
            }
            connection.release();
        });
    });
}

getReservationL = function (room_id, start_date, end_date, callback) {
    var reservationData;
    var reservationSql = 'SELECT d.id,d.create_date,d.update_date,d.room_id,d.status_id,d.start_date,d.end_date, ' +
        ' s.name as status_name,a.id as reservation_id,a.person_no as person_no, p.first_name,p.last_name,p.email ' +
        ' FROM reservation_detail d ' +
        ' inner join reservation_status s on d.status_id=s.id ' +
        ' inner join reservation a on d.reservation_id=a.id ' +
        ' inner join person p on a.person_no=p.personal_no ' +
        ' where d.room_id=? and d.start_date >? and d.start_date<?';
    pool.getConnection(function (err, connection) {
        connection.query(reservationSql, [room_id, start_date, end_date], function (error, row, fields) {
            if (err) {
                callback(err, null);
            } else {
                reservationData = row;
                callback(null, reservationData);
            }
            connection.release();
        });
    });
}

    pool.getReservation = function (branch_id, start_date, end_date, callback) {
        var categoryData;
        var roomData;
        var reservationData;
        getCategory(branch_id, function (err, data) {
            if (err) {
                callback(err, categoryData);
            } else {
                categoryData = data;
                console.log('c', categoryData);

                for (var i = 0; i < categoryData.length; i++) {
                    console.log('c', categoryData[i].name);

                    getRoom(branch_id, categoryData[0].id, function (err, data) {
                        if (err) {
                            callback(err, null);
                        } else {
                            console.log('r', data);
                            categoryData[0].room = data;

                                for (var i = 0; i < categoryData.length; i++) {

                                    getReservationL(data[0].id, start_date, end_date, function (err, data) {
                                        if (err) {
                                            callback(err, null);
                                        } else {
                                            console.log('res', data);
                                           // callback(null, data);
                                        }
                                    });

                                }


                        }

                    });

                }
                console.log('call', categoryData);
                callback(null, categoryData);
            }
        });

    }

每个步骤后的结果应该是这样的:

1)第一次查询后

[
        {
            "id": 64,
            "name": "VIP",
            "price": 116.5,
            "currency": "USD"
        },
        {
            "id": 74,
            "name": "SUPER VIP",
            "price": 110,
            "currency": "EUR"
        },
        {
            "id": 84,
            "name": "DOUBLE",
            "price": 110,
            "currency": "GEL"
        }
    ]

2)第二次查询后

[
    {
        "id": 64,
        "name": "VIP",
        "price": 116.5,
        "currency": "USD"
        room: [
                {
                   id:1,
                   name:'test'
                },
                {
                   id:2,
                   name:'test2'
                }
        ]
    },
    {
        "id": 74,
        "name": "SUPER VIP",
        "price": 110,
        "currency": "EUR"
        room: [
                {
                   id:3,
                   name:'test'
                },
                {
                   id:4,
                   name:'test2'
                }
        ]
    },
    {
        "id": 84,
        "name": "DOUBLE",
        "price": 110,
        "currency": "GEL"
        room: [
                {
                   id:5,
                   name:'test'
                },
                {
                   id:6,
                   name:'test2'
                }
        ]
    }
]

3)在第三次也是最后一次查询之后

[
    {
        "id": 64,
        "name": "VIP",
        "price": 116.5,
        "currency": "USD"
        room: [
                {
                   id:1,
                   name:'test',
                   reservation:[
                     {
                      id:1,
                      name:'aaa'
                     },
                     {
                      id:1,
                      name:'aaa'
                     }
                   ]
                },
                {
                   id:2,
                   name:'test2',
                   reservation:[
                     {
                      id:1,
                      name:'aaa'
                     },
                     {
                      id:1,
                      name:'aaa'
                     }
                   ]
                }
        ]
    },
    {
        "id": 74,
        "name": "SUPER VIP",
        "price": 110,
        "currency": "EUR"
        room: [
                {
                   id:3,
                   name:'test',
                   reservation:[
                     {
                      id:1,
                      name:'aaa'
                     },
                     {
                      id:1,
                      name:'aaa'
                     }
                   ]
                },
                {
                   id:4,
                   name:'test2',
                   reservation:[
                     {
                      id:1,
                      name:'aaa'
                     },
                     {
                      id:1,
                      name:'aaa'
                     }
                   ]
                }
        ]
    },
    {
        "id": 84,
        "name": "DOUBLE",
        "price": 110,
        "currency": "GEL"
        room: [
                {
                   id:5,
                   name:'test',
                   reservation:[
                     {
                      id:1,
                      name:'aaa'
                     },
                     {
                      id:1,
                      name:'aaa'
                     }
                   ]
                },
                {
                   id:6,
                   name:'test2',
                   reservation:[
                     {
                      id:1,
                      name:'aaa'
                     },
                     {
                      id:1,
                      name:'aaa'
                     }
                   ]
                }
        ]
    }
]

这只是结构

0 个答案:

没有答案