Mysql 当元素数大于 1 时与 join 相反
Mysql opposite of join when element count is more than 1
我需要做与内连接相反的事情。
我有例如 table "TypeComponent"。我添加了 MaxAllowed
列
IDTypeElement Type MaxAllowed
Type1 battery 1
Type2 temperature 1
Type3 pressure 3
我有 table 变送器,其中我只能有 1 个电池、1 个温度和 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",我希望能够在我的列表框中仅获得允许的组件。在这里,我的意思是,列表框必须只包含 "Type2"(temperature) 和 "Type3"(pressure),因为我们只允许有一个 "Type1"(battery) 而我们已经有了一个。另一方面,我们可以有 3 个 "Type3"(pressure) 而我们只有一个。
所以,我尝试使用该查询
SELECT IDTypeElement from typeelement
WHERE IDTypeElement not in (SELECT IDTypeElement FROM transmitter WHERE IDTransmitter="B")
我的问题是,我希望能够获取 "Type3",因为我们允许有 3 次 "Type3",但是通过该查询,我只能获取 "Type2"...有没有办法告诉一个元素的限制?有人可以帮我吗?
希望你理解我的问题(和我的英语)。
如果我以 IDTranmistter 为例: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 @ http://sqlfiddle.com/#!9/c8743/5
你可以做左连接
表格:
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)
我需要做与内连接相反的事情。
我有例如 table "TypeComponent"。我添加了 MaxAllowed
列IDTypeElement Type MaxAllowed
Type1 battery 1
Type2 temperature 1
Type3 pressure 3
我有 table 变送器,其中我只能有 1 个电池、1 个温度和 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",我希望能够在我的列表框中仅获得允许的组件。在这里,我的意思是,列表框必须只包含 "Type2"(temperature) 和 "Type3"(pressure),因为我们只允许有一个 "Type1"(battery) 而我们已经有了一个。另一方面,我们可以有 3 个 "Type3"(pressure) 而我们只有一个。
所以,我尝试使用该查询
SELECT IDTypeElement from typeelement
WHERE IDTypeElement not in (SELECT IDTypeElement FROM transmitter WHERE IDTransmitter="B")
我的问题是,我希望能够获取 "Type3",因为我们允许有 3 次 "Type3",但是通过该查询,我只能获取 "Type2"...有没有办法告诉一个元素的限制?有人可以帮我吗?
希望你理解我的问题(和我的英语)。
如果我以 IDTranmistter 为例: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 @ http://sqlfiddle.com/#!9/c8743/5
你可以做左连接
表格:
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)