我可以从WordPress数据库中获取这样的特定类别的链接(为了清楚起见,我在SQL部分添加了换行符):
$category1 = 'stuff';
$category2 = 'other_stuff';
$q = 'select * from wp_links l
inner join wp_term_relationships r on l.link_id = r.object_id
inner join wp_term_taxonomy using (term_taxonomy_id)
inner join wp_terms using (term_id)
where taxonomy = "link_category"
and name = '.$category1;
$results = $wpdb->get_results($q);
如何检索 $category1
和$category2
中的链接(我的意思是两个类别,而不是 < / em> category)?
答案 0 :(得分:0)
你试过OR?
$q = 'select * from (select * from wp_links l
inner join wp_term_relationships r on l.link_id = r.object_id
inner join wp_term_taxonomy using (term_taxonomy_id)
inner join wp_terms using (term_id)
where taxonomy = "link_category"
and name = '.$category1.') as t1 join (select * from wp_links l
inner join wp_term_relationships r on l.link_id = r.object_id
inner join wp_term_taxonomy using (term_taxonomy_id)
inner join wp_terms using (term_id)
where taxonomy = "link_category"
and name = '.$category2 .') as t2
on t1.term_id=t2.term_id;
我不认为这是最好的解决方案,但是你没有给我更多关于你的结构的详细信息,表格你有什么
答案 1 :(得分:0)
假设您正在搜索的name
字段位于wp_terms
表中,则有两个主要选项。
第一个只查找具有第一个类别的实体,然后使用另一个连接查找那些也具有第二个类别的实体。这个involes在两个不同的连接中使用相同的表,因此使用别名并避免使用using
关键字。
select * from wp_links l
inner join wp_term_relationships r on l.link_id = r.object_id
inner join wp_term_taxonomy t on r.term_taxonomy_id = t.term_taxonomy_id
inner join wp_terms c1 on t.term_id = c1.term_id
inner join wp_terms c2 on t.term_id = c2.term_id
where taxonomy = "link_category"
and c1.name = 'stuff'
and c2.name = 'other_stuff'
替代方案可扩展到不仅仅是两个类别,但涉及子查询......
select
l.*
from
(
select l.id from wp_links l
inner join wp_term_relationships r on l.link_id = r.object_id
inner join wp_term_taxonomy t on r.term_taxonomy_id = t.term_taxonomy_id
inner join wp_terms c on t.term_id = c.term_id
where taxonomy = "link_category"
and c.name IN ('stuff', 'other_stuff')
group by l.id
having count(distinct c.name) = 2
)
subquery
inner join wp_links l ON subquery.id = l.id
内部查询查找具有一个类别或另一个类别的所有内容,但是having子句仅允许通过列表中包含两个类别的那些。 (换句话说,两者都有。) [它还假定wp_links
表作为id
列用作唯一标识符。]