I want to fetch some attributes of a Model, but one attribute only when a condition satisfies. Is it possible to achieve this in Sequelize?
Eg. Model
id | mobile | created_by | |
---|---|---|---|
1 | row1 | 12345 | a |
2 | row2 | 12345 | b |
3 | row3 | 12345 | a |
4 | row4 | 12345 | b |
I want to fetch attributes id, email, mobile when created_by = a otherwise fetch only attributes id and email.
CodePudding user response:
Edit: Nothing, just comments below ↓
CodePudding user response:
You cannot conditional select a column, but you control whether to select the value or some other value with a case
expression; for this select null instead. So
select id
, email
, case when created_by = 'a'
then mobile
else null
end as mobile
from model;
Sorry but I do not know Sequelize, but translating should not be difficult.