2017-06-29 36 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); 
    }); 
相關問題