Jump to content

Never used a UNION or a JOIN, but I need to


jlipin3

Recommended Posts

I'm using WordPress.  I want to select a group of id's in one table, and use those id's to select a bunch of posts in another table, and only retrieve the most recent post.

 

So, theoretically, I want to do this:

 

$article_ids_query = mysql_query("SELECT `object_id` FROM `wp_term_relationships` WHERE `term_taxonomy_id`='" . $term_taxonomy_id[0] . "'"); //this will retrieve a bunch of `object_id's`

 

$articles = mysql_query("SELECT `post_title`, `post_content` FROM `wp_posts` WHERE `ID`='" . $article_ids_query . "'");

 

 

The obvious problem is that the $article_ids_query is going to be a result set.

 

How could I do it so that I use the query in $article_ids_query to be part of the $articles query.

 

This is what I could come up with using a "UNION", but I've never used UNION before:

 

(SELECT `object_id` as `a` FROM `wp_term_relationships` WHERE `term_taxonomy_id`='" . $term_taxonomy_id[0] . "') UNION (SELECT `post_content`, `post_title` FROM `wp_posts` WHERE `ID`=`a` AND `post_type`='post') LIMIT 1 ORDER BY `post_date` ASC

 

The help is obviously hugely appreciated, freaks.  Josh

Link to comment
Share on other sites

Actually, you just want a join here:

 

$articles = mysql_query("SELECT `post_title`, `post_content` FROM `wp_posts` JOIN `wp_term_relationships`ON ( `wp_term_relationships`.`object_id` = `wp_posts`.`ID` ) WHERE `term_taxonomy_id`='" . $term_taxonomy_id[0] . "'");

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.