我在mysql中有3张桌子:
卡片:
id | name
1 | alpha
2 | beta标签:
id | name
1 | a
2 | btag_link:
id | card | id
1 | 1 | 1
2 | 2 | 1
3 | 2 | 2我想取回所有不包括特定标签的卡片。CI模式:
function search($_tag) {
$this->db->select('card.id');
$this->db->join('tag_link', 'card.id = tag_link.card');
$this->db->where_not_in('tag_link.tag', $_tag);
$this->db->group_by('card.id');
$query = $this->db->get('card');
return $query;
}对于标签'2‘,这将返回卡片'1’如预期。然而,卡'2‘也被错误地返回,因为在tag_link连接卡'2’与标签'1‘的一个条目。
我考虑使用上面的函数获得第一个点击数组,然后在php中减去另一个数组,其中包含所有卡片,包括我不感兴趣的标记。然而,这个解决方案感觉非常笨拙。解决这个问题的最有效的方法是什么?
谢谢你,奇图
发布于 2013-10-04 05:18:19
使用TheWolf的答案并将其转换为active_record。记住,CI不支持子查询(尽管可以使用库)。
$this->db->select('id');
$this->db->where('id NOT IN (SELECT card FROM tag_link WHERE tag IN $tags)', NULL, FALSE);
$query = $this->db->get('cards');https://stackoverflow.com/questions/19170048
复制相似问题