简体   繁体   中英

SQL to query drupal nodes with multiple taxonomies

Good morning all. I've read some of the suggested question before posting this but seems like no one has my same issue (probably and inidicator of how bad I am in Drupal and coding in general)

I need to write a query that returns all the nodes with TWO SPECIFIC taxonomies associated to it (of which I know the IDs), but it seems I don't know the right syntax cause I just manage to get it work with ONE term id.

Here's what I have so far (and works)

SELECT * FROM node 
INNER JOIN term_node AS tn ON node.vid = tn.vid 
LEFT JOIN content_type_extra_content AS xc ON node.vid = xc.vid 
WHERE tn.tid IN (SELECT th.tid FROM term_hierarchy AS th WHERE th.tid = '146')

That '146' is the id of the first taxonomy term I need to check (call it "shoes") Now I have to check that the node has also the taxonomy id '223' (call it "season")

I've tried different solutions with no avail. I'm pretty sure the solution is under my nose but at the moment I can't wrap my head around it.

Please note that the taxonomies are in different vocaboularies and they are at level-0

Thanks in advance for any help

If I understand you correctly, you want the nodes which have 2 specific terms (shoes and season), then try something like this:

SELECT * FROM node 
INNER JOIN term_node AS tn ON node.vid = tn.vid 
LEFT JOIN content_type_extra_content AS xc ON node.vid = xc.vid 
WHERE tn.tid IN ('146','223')
GROUP BY node.vid
HAVING count(*) = 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM