快速查询SQL

时间:2013-08-28 18:07:32

标签: mysql sql express

我正在动态尝试将表的名称传递给SQL查询,在下面的快速代码中

背景资讯::

  • 我作为(键,值)传递的是将成为的字符串 sql数据库中表的名称
  • 我为什么要根据动态动态选择表格 客户要求

我面临的问题::

  • 显然我不是正确的sql查询sunig
  • 如何解决此问题

[ExpressCode]

app.get('/RestaurantDesc/:Key',function(request,response,next){

    var keyName=request.params.Key;
    var name_of_restaurants, RestaurantTimings;
    async.series( [
        // Get the first table contents
        function ( callback ) {
            connection.query('SELECT * FROM keyName', function(err, rows, fields)
                {
                        console.log('Connection result error '+err);
                        name_of_restaurants = rows;
                        callback();
                });
        },
        // Get the second table contents
        function ( callback ) {
        connection.query('SELECT * FROM RestaurantTimings', function(err, rows, fields)

            {
                    console.log('Connection result error '+err);
                    RestaurantTimings = rows;
                    callback();
            });
        }

   // Send the response
], function ( error, results ) {
    response.json({
        'restaurants' : name_of_restaurants,
        'RestaurantTimings' : RestaurantTimings
    });
} );
} );

1 个答案:

答案 0 :(得分:0)

keynameRestaurantTimings视为表名。试试这个: -

app.get('/RestaurantDesc/:Key',function(request,response,next){

    var keyName=request.params.Key;
    var name_of_restaurants, RestaurantTimings;

    async.series( [
        // Get the first table contents
        function ( callback ) {
            connection.query('SELECT * FROM ', keyName, function(err, rows, fields)
                {
                        console.log('Connection result error '+err);
                        name_of_restaurants = rows;
                        callback();
                });
        },
        // Get the second table contents
        function ( callback ) {
        connection.query('SELECT * FROM ', RestaurantTimings, function(err, rows, fields)

            {
                    console.log('Connection result error '+err);
                    RestaurantTimings = rows;
                    callback();
            });
        }

   // Send the response
], function ( error, results ) {
    response.json({
        'restaurants' : name_of_restaurants,
        'RestaurantTimings' : RestaurantTimings
    });
} );
} );