• I have no idea if anyone can help me here, but I’ve been trying for forever to figure this out and I can’t. I’m using the TwitterLink comments plugin to add an extra line to the comments form for additional information. On a certain page I’m trying to pull up the comment along with the data for the TwitterLink field. However it’s being stored in a separate table and I don’t know how to pull it in. My knowledge of mysql is basic at best, but I see a left outer join in the code here and I’m thinking maybe I need to add another one?

    The table is named wptwitipid and the column I need is called twitd. I tried adding LEFT OUTER JOIN $wpdb->wptwitipid ON ($wpdb->twitid.comment_post_ID = $wpdb->posts.id) but I could be way off and it’s not working.

    If anyone has even a hint for me I’d really appreciate it!!

    My code:

    $sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID,
    	comment_post_ID, comment_author, comment_date_gmt, comment_approved,
    	comment_type,comment_author_url,
    	SUBSTRING(comment_content,1,50) AS com_excerpt
    	FROM $wpdb->comments
    
    	LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID =
    	$wpdb->posts.ID)	
    
    	WHERE comment_approved = '1' AND comment_type = '' AND
    	post_password = '' AND comment_post_ID = 90
    	ORDER BY comment_date_gmt DESC LIMIT 14";
    
    	$comments = $wpdb->get_results($sql);
    $comments = array_reverse($comments);
    	$output = $pre_HTML;

Viewing 11 replies - 1 through 11 (of 11 total)
  • I think you need to put
    LEFT OUTER JOIN $wpdb->wptwitipid ON ($wpdb->wptwitipid.twitd = $wpdb->posts.ID)

    Thread Starter Jess

    (@jessn)

    Thank you for your help! Your answer seems to make a lot of sense. Unfortunately it doesn’t work either. When I include that line no comments appear at all

    Can you please write down the whole query.

    Thread Starter Jess

    (@jessn)

    Sure!

    <?php
    global $wpdb;
    
    	$sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID,
    	comment_post_ID, comment_author, comment_date_gmt, comment_approved,
    	comment_type,comment_author_url,
    	SUBSTRING(comment_content,1,50) AS com_excerpt
    	FROM $wpdb->comments
    
    	LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID =
    	$wpdb->posts.ID)	
    
    	WHERE comment_approved = '1' AND comment_type = '' AND
    	post_password = '' AND comment_post_ID = 90
    	ORDER BY comment_date_gmt DESC LIMIT 14";
    
    	$comments = $wpdb->get_results($sql);
    $comments = array_reverse($comments);
    	$output = $pre_HTML;
    	$output .= "\n
    <ul class='sonnet-lines'>";
    
    	foreach ($comments as $comment) {
    	$output .= "\n<li>". strip_tags($comment->com_excerpt)."<br />
    	". strip_tags($comment->comment_author) ."</li>";
    	}
    	$output .= "\n</ul>
    ";
    	$output .= $post_HTML;
    echo $output; ?>

    Thank you again

    Have you checked your all comments are approved.

    Is there any comment under post id 90.
    You can also check by removing this condition AND comment_post_ID = 90

    Thread Starter Jess

    (@jessn)

    Oh that’s because I’m pulling up comments for that specific post and yeah, there are several approved ones. That query above works just fine, it’s when I try to add to it that it breaks. I can’t figure out how to include the wptwitipid table…

    Try this one

    $sql = "SELECT DISTINCT ID, twitid, post_title, post_password, comment_ID,
    	comment_post_ID, comment_author, comment_date_gmt, comment_approved,
    	comment_type,comment_author_url,
    	SUBSTRING(comment_content,1,50) AS com_excerpt
    	FROM $wpdb->comments
    
    	LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID =
    	$wpdb->posts.ID)
    LEFT OUTER JOIN $wpdb->wptwitipid ON ($wpdb->wptwitipid.email = $wpdb->comments.comment_author_email)	
    
    	WHERE comment_approved = '1' AND comment_type = '' AND
    	post_password = '' AND comment_post_ID = 90
    	ORDER BY comment_date_gmt DESC LIMIT 14";

    Thread Starter Jess

    (@jessn)

    No that killed it too. No comments appear at all. So strange. I tried the whole part, then just the new left outer join, then just the sql statement.

    Now this should work for sure

    $sql = “SELECT DISTINCT $wpdb->posts.ID, twitid, post_title, post_password, comment_ID,
    comment_post_ID, comment_author, comment_date_gmt, comment_approved,
    comment_type,comment_author_url,
    SUBSTRING(comment_content,1,50) AS com_excerpt
    FROM $wpdb->comments

    LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID =
    $wpdb->posts.ID)
    LEFT OUTER JOIN {$wpdb->prefix}wptwitipid ON ({$wpdb->prefix}wptwitipid.email = $wpdb->comments.comment_author_email)

    WHERE comment_approved = ‘1’ AND comment_type = ” AND
    post_password = ” AND comment_post_ID = 90
    ORDER BY comment_date_gmt DESC LIMIT 14″;

    Thread Starter Jess

    (@jessn)

    Yes!!! That’s perfect!! Ah and I see the wpdb->prefix there now. That makes sense. Now if I can just figure out how to display the info. This doesn’t do anything: strip_tags($comment->twitlinkid)

    Kapil, thank you so so much for all of your help! You’re awesome.

    $wpdb->posts.ID is also there because id column also exists in wptwitipid table.

    You are most welcome

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘mysql query of comments – merge with another table?’ is closed to new replies.