Knex从两个数据库连接表

时间:2017-06-29 09:15:26

标签: mysql node.js knex.js

我是Node.js的新手。我正在使用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());
    });

1 个答案:

答案 0 :(得分:1)

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);
  });
相关问题