有 2 个表:
A(`A_id`,`A_name`)
和
B(`B_id`,`A_id`,`B_kind`)
B_kind
列是 ENUM('type 1','type 2')
。那么,我怎样才能得到所有 A
行,这些行在表 B
中有一个条目,带有 B_kind='type 1'
但没有 B_kind='类型 2'
.
在 php 中的表示:
$A = array();
$B = array();
$sample = array();
foreach( $A as $key => $value )
{
if( find_type1($value,$B) && ! find_type2($value,$B) )
$sample[] = $A;
}
请帮忙,我该怎么做?
最佳答案
您可以使用exists
和not exists
:
select *
from A
where exists
(
select *
from B
where B.A_Id = A.A_Id
and B.B_kind = 'type 1'
)
and not exists
(
select *
from B
where B.A_Id = A.A_Id
and B.B_kind = 'type 2'
)
关于php - Mysql多选排除行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3685794/