node-mysql仅在两个表联合后从第一个表中提供数据

时间:2015-08-17 17:54:55

标签: mysql node.js node-mysql node-modules

我有两个表 toponlinestores 品牌    在 MySQL WorkBench 中运行查询(如下所列)时,它们正常工作但是使用 node-mysql 我只从第一个表中获取数据

set @toponlinestores=3;
set @brands=2;
SELECT * from (
SELECT 
'toponlinestores' as 'table_name',store_name,tl.id as website_id, 
@toponlinestores * (MATCH(store_name) AGAINST ('f*' IN BOOLEAN MODE)) as     relevance
from toponlinestores tl
UNION
SELECT 
'brands' as 'table_name',brand_name,
br.id as brand_id,
@brands * (MATCH(brand_name) AGAINST ('f*' IN BOOLEAN MODE)) as relevance
from brands br
)
as sitewide WHERE relevance > 0;

**result :
1.  toponlinestores flipkart    2   3.051822781562805
2.  brands          fossil_mini 18  1.6311430931091309**

但当我使用node-mysql

var sql1 = 'set @toponlinestores=3';
    var sql2 = '@brands=2';
    var sql3 = "SELECT * from ("+
    "SELECT "+
      " 'toponlinestores' as 'table_name',store_name as name,id as id, "+
      " @toponlinestores * (MATCH(store_name) AGAINST ('f*' IN BOOLEAN MODE)) as relevance"+
      " from toponlinestores "+
    " UNION "+
    "SELECT "+
     " 'brands' as 'table_name',brand_name as name, " +
      "id as id, " +
      " @brands * (MATCH(brand_name) AGAINST ('f*' IN BOOLEAN MODE)) as relevance "+
               " from brands"+
  ")"+
  "as sitewide WHERE relevance > 0";


  var query1 = connection.query(sql1);
    query1.on("error", function (err) {
        // handle error
    });
    query1.on("end", function () {
      var query2 = connection.query(sql2);
      query2.on("error", function (err) {
          // handle error
      });
      query2.on("end", function () {
        connection.query(sql3,function(err,res_data)
        {
                 if(err) console.log(err);
                 else
                 {
                  res.json(res_data);
                  console.log(res_data);
                 }

        });

      });
  });

结果是: 1. toponlinestores flipkart 2 3.051822781562805

我没有从第二个表中获取数据,但是当我在 MySQL WorkBench 中执行相同的查询时,我从两个表中获取数据。

请帮助 - 提前谢谢。

1 个答案:

答案 0 :(得分:0)

试试这个: -

var sql2 = 'set @brands=2';

取代

var sql2 = '@brands=2';