当元素计数大于1时,Mysql与join相反

当元素计数大于1时,Mysql与join相反,mysql,inner-join,Mysql,Inner Join,我需要做与内部连接相反的操作 例如,我有TableTypeComponent。我已经添加了MaxAllowed列 IDTypeElement Type MaxAllowed Type1 battery 1 Type2 temperature 1 Type3 pressure 3 我有一个台式变送器,其中只有一个电池、一个温度和三个压力元件 ID

我需要做与内部连接相反的操作

例如,我有TableTypeComponent。我已经添加了MaxAllowed列

IDTypeElement    Type           MaxAllowed
   Type1         battery            1
   Type2         temperature        1
   Type3         pressure           3
我有一个台式变送器,其中只有一个电池、一个温度和三个压力元件

ID    IDTransmitter   IDTypeElement   
1         A              Type1
2         A              Type2
3         A              Type3
4         A              Type3
5         A              Type3

6         B              Type1
7         B              Type3
当我们向发送器添加一个组件时,我需要能够删除我们已有的TypeElement。例如,发射机B,我希望能够在我的列表框中仅获取允许的组件。这里,我的意思是,列表框必须只包含Type2temperature和Type3pressure,因为我们只允许有一个Type1battery,而我们已经有了一个。另一方面,我们可以有3种类型3压力,但我们只有一种

所以,我尝试处理这个查询

SELECT IDTypeElement from typeelement
WHERE IDTypeElement not in (SELECT IDTypeElement FROM transmitter WHERE IDTransmitter="B")
我的问题是,我希望能够抓取Type3,因为我们允许有3次Type3,但通过该查询,我只得到Type2。。。有没有办法判断一个元素的极限?有人能帮我吗

希望你能理解我的问题和我的英语


如果我以idtranmister:B为例,使用与上面类似的查询,我希望在我的列表框中有:Type2和Type3

以下类似的功能:

Select e.IDTypeElement
from TypeComponent e LEFT JOIN
(
select IDTransmitter, IDTypeElement, count(*) as used
from Transmitter 
group by IDTransmitter,IDTypeElement
) t
on e.IDTypeElement = t.IDTypeElement
and t.IDTransmitter = 'B'
where (e.MaxAllowed-ifnull(t.used,0))>0
请检查sqlfiddle@

您可以进行左连接

表:

查询:


请编辑您的问题并添加您的预期或期望输出,好吗?您需要将该业务规则存储在其中一个表中,即电池中每个部件需要多少数量。如果没有它,编写这样的queryOk将非常困难,我已经添加了期望的结果@Harsh,我在TypeElement表中添加了一个Max Allowed列。。。但是我该怎么处理那个专栏呢?请检查我的答案,然后用sqlfiddle哦。。好吧,看起来很残酷。
mysql> select * from typecomponent
    -> ;
+---------------+-------------+------------+
| idtypeelement | type        | maxallowed |
+---------------+-------------+------------+
| Type1         | battery     |          1 |
| Type2         | temperature |          1 |
| Type3         | pressure    |          3 |
+---------------+-------------+------------+
3 rows in set (0.00 sec)

mysql> select * from transmitter;
+----+---------------+---------------+
| id | idtransmitter | idtypeelement |
+----+---------------+---------------+
|  1 | A             | Type1         |
|  2 | A             | Type2         |
|  3 | A             | Type3         |
|  4 | A             | Type3         |
|  5 | A             | Type3         |
|  6 | B             | Type1         |
|  7 | B             | Type3         |
+----+---------------+---------------+
7 rows in set (0.00 sec)
select idtypeelement from (select tc.idtypeelement, tc.maxallowed, count(t.idtypeelement) as usedComponents from typecomponent tc left join transmitter t on t.idtypeelement = tc.idtypeelement and t.idtransmitter='B' group by 1,2 having usedComponents < tc.maxallowed) t;
+---------------+
| idtypeelement |
+---------------+
| Type2         |
| Type3         |
+---------------+
2 rows in set (0.00 sec)