2017-06-29 30 views
0

我是Node.js的新手。我正在使用Knex进行查询。我需要从两个不同的数据库中加入两张表。有谁能告诉我这是可能的吗?Knex从两个数据库中加入表格

knex.select('id', 'full_name','email', 'mobile_country_code', 'mobile', knex.raw('1 as active_status')).from('users').where(whereData).union(function() { 
     this.select('id','full_name', 'email', 'mobile_country_code', 'mobile', knex.raw('0 as active_status')).from('users_temp').where(whereData); 
    }).then(function(data) { 
     next(null, data); 
    }).catch(function(err) { 
     next(err.toString()); 
    }); 
+0

您可以加入普通SQL查询,你要创建的简单情况? –

+0

( SELECT u1.id,u1.full_name,u1.email,u1.mobile_country_code,u1.mobile,1 AS active_status,u2.user_timeline_pic FROM db1.users AS u1 JOIN db2.users AS u2 ON u1.id = u2.id ) UNION( SELECT ID,FULL_NAME,电子邮件,mobile_country_code,移动,0 AS active_status, '默认/ user_profile_pic.png' AS user_timeline_pic FROM db1.users ) – Pillai

+0

我需要的是上面给出。感谢您的回复@MikaelLepistö – Pillai

回答

0
knex 
    .select(
    'users.id','users.full_name', 'users.email', 
    'users.mobile_country_code', 'users.mobile', 
    'loc.user_profile_pic', 'loc.user_timeline_pic' 
) 
    .from('users') 
    .leftJoin(config.project_db + '.users as loc', 'loc.id', '=', 'users.id') 
    .where(whereData).union(function() { 
    this.select(
     'id','full_name', 
     knex.raw('\''+constants.images.default_user_profile_pic+'\' as user_profile_pic, \''+constants.images.default_user_timleline_pic+'\' as user_timeline_pic'), 
     'email', 'mobile_country_code', 'mobile' 
    ) 
    .from('users_temp').where(whereData); 
    }); 
相关问题