Knex js How to apply union on 2 tables which has different number of columns

Viewed 176

I have 2 tables with diff columns as shown below

  • Table - A Columns

Name, Age, Gender, email_id

  • Table -B Columns

course, course_type, university

  • How to convert below mentioned query in Knexjs

SELECT NAME, EMAIL_ID, NULL AS COURSE, NULL AS UNIVERSITY FROM TABLEA UNION SELECT NULL AS NAME, NULL AS EMAILID, COURSE, UNIVERSITY FROM TABLEB

1 Answers

I tried below mentioned knex js query but getting error

  • Knexjs query

myDB .select('NAME', 'EMAIL_ID', {'NULL':'COURSE'}, {'NULL':'UNIVERSITY'}) .from('TABLEA') .union(canonDB.select({'NULL':'NAME'},{'NULL':'EMAILID'}, COURSE',UNIVERSITY}).from('TABLEB'))

  • Error :

Unknown column 'NULL' in 'field list'

Related