Let's say I have a MySQL table like this:

[![enter image description here](https://i.stack.imgur.com/GsLQ9.png)](https://i.stack.imgur.com/GsLQ9.png)

How can I write the following MySQL query using sequelize?

```
select mt.JobId 
from   MyTable as mt join MyTable as mt2 on mt2.JobId = mt.JobId
where  mt.TaskSeq = 0 and mt.TaskState = 'Done' and mt2.TaskSeq = 1 
and mt2.TaskState = 'New'

```

This query returns all the `JobId`s whose `taskA` is in `Done` state and `taskB` is in `New` state.

In this case, it returns `qwert`, and `zxcv`.

After reading a similar stackoverflow question [here](https://stackoverflow.com/questions/20460270/how-to-make-join-querys-using-sequelize-in-nodejs). I get a little confused, because I only have 1 table. Should I use `hasMany` and `belongsTo` in order to create a `join` query in sequelize?

The following is part of my node.js scripts for creating this table

```
var MyTable = conn.define(
'MyTable',
{
    JobId: {
    type: Sequelize.STRING,
    unique: true
    },
    TaskName: {
    type: Sequelize.STRING
    },
    TaskSeq: {
    type: Sequelize.INTEGER
    },
    TaskState: {
    type: Sequelize.STRING
    }
},
{
    collate: 'utf8mb4_bin'
}
)

MyTable.sync()
module.exports = {
    MyTable, conn
}

```

And this is my sequelize query, which fails to return the `JobId`s I need.

```
MyTable.belongsTo(MyTable, {foreignKey: 'id'})
MyTable.findAll({
attributes: ['JobId'],
where: {
    TaskSeq: 0,
    TaskState: 'DONE'
},
include: [{
    model: MyTable,
    attributes: ['JobId'],
    where: {
        JobId: sequelize.col('MyTable.JobId'),
        TaskSeq: 1,
        TaskState: 'NEW'
    }
}]
}).then(result => {
    console.log(result)
})

```