简体   繁体   中英

Joining three tables together based on common ids

在此处输入图片说明

What I'm looking to generate is the wp_tylerposts table as it is, with the same specifications as the query (selecting only those with post_type='sponsor' , but I'd like to reference the other two tables ( object_id from wp_tylerterm_relationships and the corresponding name from wp_tylerterms ) so that added on to wp_tylerpostswould be the corresponding name value from wp_tylerterms (added to the table would be "Gold Sponsors", for example).

Hopefully that makes sense. I'm sure there's a pretty simple solution, and I've tried my hand at some join queries without any luck... haven't done any of the stuff in a long time. Any help would be much appreciated!

Edit: I've come closer, I think, but I still can't retrieve the "name" column value from wp_tylerterms, here's what I have:

SELECT 
    c.ID, c.post_title, a.name
FROM 
    wp_tylerposts c
LEFT JOIN
    wp_tylerterm_relationships b
ON
    c.ID = b.object_id
LEFT JOIN
    wp_tylerterms a
ON
    b.object_id = a.term_id
WHERE
    c.post_type = 'sponsor'

In second join , you use b.object_id instead of b.term_taxonomy_id . Your query should look like this:

SELECT 
    c.ID, c.post_title, a.name
FROM 
    wp_tylerposts c
LEFT JOIN
    wp_tylerterm_relationships b
ON
    c.ID = b.object_id
LEFT JOIN
    wp_tylerterms a
ON
    b.term_taxonomy_id = a.term_id
WHERE
    c.post_type = 'sponsor'

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