Closed shaangidwani closed 1 year ago
You can use the as
option in join
.
e.g- here column customerId is present in both table - in this situation you can use as
to create alias of the column.
from: table1 name,
join: {
with: table2_name,
on: "table1.common_field=table2.common_field",
as: {
customerId: table2_customerId
}
}
});
Association query message table appendixId Multiple messages are associated with the uuid of a Chatappendixs table. Only the first one can find the Chatappendixs content, and the remaining few messages cannot be found @ujjwalguptaofficial ujjwalguptaofficial
getListWithMsg(data, limit, name) { this.tableName = name ? name : this.tableName let as = {} Object.keys(MessageTable.columns).forEach(key => { if (Object.keys(ChatappendixTable.columns).includes(key)) { as[key] = ChatappendixTable.name + key } }) let body = { from: this.tableName, where: data, order:{ by: 'Messages.timestamp', type: 'desc' //supprted sort type is - asc,desc }, join: { with: 'Chatappendixs', on: 'Messages.appendixId=Chatappendixs.uuid', type: 'left', as: as } } if(limit) { body.limit = limit } return connection.select(body); }
Thanks @ujjwalguptaofficial its working fine.
Again thanks for your quick response
Welcome, please stars the repo.
Join with two tables with select specific columns or all columns
//Join with two tables with multiple relational/duplicate columns
We have two tables and both have multiple columns with the same name ex: We are joining with PatientID which are exits on both the tables and another column with ExternalPatientID which are same name with different data in both the tables but it gives error that ExternalPatientID is exited for both the tables.
// Same columns are exits in both the tables at the time of joining
Query :
return this.connection.select({ from: this.tableName, join: { with: 'Patient', on: "Patient.PatientID=Encounter.PatientID" } })
Both the tables have column name "OLPatientID" and when we execute its gives the error that : column OLPatientID exists in both table Encounter & Patient
Is there any way to select a specific column or avoid this issue?