DeniedHashTag
DeniedHashTag

Reputation: 23

Can someone help me in convert this MySql query to sequelize?

I am new to sequelize. I am not sure how to convert this MySql query so that I can use it in my node.js file.

MySql query:

SELECT Rtrim(Ltrim(childstatus)),TIMESTAMPDIFF(d,dob,now(3)) 
INTO @childstatus, @Ageday 
FROM childdetails where registno=@registno

I have sequelize model for childdetails. I am not sure how to structure this query.

Upvotes: 0

Views: 95

Answers (1)

Anatoly
Anatoly

Reputation: 22783

You can use Sequelize.fn to call these two functions indicating them in attributes option like this:

const details = await ChildDetials.findAll({
  attributes: [
  [Sequelize.fn('RTrim', Sequelize.fn('LTrim', Sequelize.col('childstatus'))), 'childsttaus'],
  [Sequelize.fn('TIMESTAMPDIFF', Sequelize.literal('d'), Sequelize.col('dob'), Sequelize.fn('now', 3)), 'Ageday']
  ],
  where: {
    registno: registno
  }
})

Upvotes: 1

Related Questions