具体来自wordpress的用户元表。我试图让所有没有某个metakey的用户
所以例如
user_id - 1 2 3 3 4 2 1
meta_key - a a a b a c c b
met_value - etc
所以说这个例子我想让所有没有数据库条目的用户使用b的元键。这将排除3和1 ... sql查询应该只返回用户2和4.我知道如何轻松地执行此操作,找到所有具有B的元键的用户,但我不知道如何写获取其他所有用户的查询。
由于
答案 0 :(得分:0)
一种方法是not eixsts
:
select u.*
from users u
where not exists (select 1
from metatable m
where meta_key = 'b' and m.user_id = u.user_id
);
如果您没有单独的用户表:
select distinct m.user_id
from metatable m
where not exists (select 1
from metatable m2
where m2.meta_key = 'b' and m2.user_id = m.user_id
);
我认为有问题的元表是wp_usermeta
。
答案 1 :(得分:0)
$users_with_meta = get_users([
'meta_key' => 'some_key',
'fields' => ['ID'],
]);
$users_with_meta = [];
foreach($users_with_meta as $user)
$users_with_meta[] = $user->ID;
$users_without_meta = get_users(['exclude' => $users_with_meta]);