[英]How to get the intersection of 2 mysql queries?
Suppose I have a MySQL table which looks like this: 假设我有一个如下所示的MySQL表:
Each job in the table contains 3 tasks. 表中的每个作业包含3个任务。
How can I get all the JobId
s whose taskA
is in Done
state and taskB
is in New
state? 如何获取其
taskA
处于Done
状态且taskB
处于“ New
Done
状态的所有JobId
?
In my case, I want a query which returns qwert
, and zxcv
. 就我而言,我想要一个返回
qwert
和zxcv
的查询。
I've come up with this query: 我想出了这个查询:
select JobId from MyTable where TaskSeq=0 and TaskState='Done'
intersect
select JobId from MyTable where TaskSeq=1 and TaskState='New';
but my version of MySQL doesn't support the intercept operator. 但是我的MySQL版本不支持拦截运算符。
My ultimate goal is to write the query in sequelize . 我的最终目标是用sequelize编写查询。 But I think I should know the MySQL query first so that I can create a sequlize query.
但是我想我应该首先了解MySQL查询,这样我才能创建一个序列化查询。
And I also wish that the sequlize query can be done in 1 function instead of multiple functions concatenated with then
. 而且,我也希望可以在1个函数中完成序列查询,而不是与
then
串联的多个函数。
Here's the SQL Fiddle to help you try the table. 这是SQL Fiddle,可以帮助您尝试使用该表。
You could just use a join: 您可以只使用联接:
select mt.JobId
from MyTable mt
join MyTable mt2 on mt2.JobId = mt.JobId
where mt.TaskSeq = 0 and mt.TaskState = 'Done' and mt2.TaskSeq = 1 and mt2.TaskState = 'New'
Here's an attempt at Sequelize on this query, however this is a guess. 这是尝试对该查询进行Sequelize的尝试,但这只是一个猜测。 Hopefully it gives you something to work with:
希望它为您提供了一些可以使用的功能:
MyTable.findAll({
attributes: ['JobId', 'TaskSeq', 'TaskState'],
include: [{
model: MyTable,
attributes: ['JobId', 'TaskSeq', 'TaskState'],
where: {
JobId: Sequelize.col('MyTable.JobId'),
TaskSeq: 1,
TaskState: 'New'
}
}],
where {
TaskSeq: 0,
TaskState: 'Done'
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.