0

Hi Im trying to run 2 querys in the same query call.

        // send the query

global $wpdb;
$commentQ = "SELECT * FROM $wpdb->comments " . $whereClause . $orderBy;
$comments = $wpdb->get_results( $commentQ, ARRAY_A);

So far it works , but when i try to run 2 querys it fails. What I wanna do is i want to run:

**SELECT *
FROM `wp_88_commentmeta`
ORDER BY `wp_88_commentmeta`.`meta_value` ASC
LIMIT 0 , 30**

Im trying to get the meta_value only from that table.

So my question is , how can i call 2 querys in the same call, and if theres i a better way to do that?

EDIT:

I want to JOIN $wpdb->comments with commentmeta. Im only trying to get the field "meta_value".

    global $wpdb;
$commentQ = "SELECT * FROM $wpdb->comments " . $whereClause . $orderBy;
$comments = $wpdb->get_results( $commentQ, ARRAY_A);

// build the string

$commentString = "";
$count = 0;
if( $comments ){
    $first = true;
    foreach( $comments as $row ){
        if( $first ){
            // column labels
            foreach ( $row as $col => $value ){
                    //$commentString .= $col . chr( 9 );

After i join the tables i want to get "meta_value" from wp_commentmeta_88 and insert it instead of "comment_author"

                    // Column Author
                if($col == 'comment_author') {
                $commentString .= 'User Meta        ';
                }           

            // Column IP
            if($col == 'comment_author_IP') {
                $commentString .= '   IP    ';
            }       

            // Column date
            if($col == 'comment_date') {
                $commentString .= '                Datum    ';
            }   

            // Column comment
            if($col == 'comment_content') {
                $commentString .= '                Meddelande   ';
            }                                           

            }
            $commentString .= chr( 13 );
            $first = false;
        }
7
  • 1
    Your second block of code does not demonstrate running two queries. It is just one query. Please provide enough code and commentary to explain the question properly. Commented Jan 11, 2013 at 15:06
  • Hi! I know my second block dont demonstrate how to run 2 queries. I gave that second block of code to show what query i want to run in the same way/time as the first. Commented Jan 14, 2013 at 9:10
  • But you don't even post how you ran the first query. Sounds like what you are asking is a pure SQL question. That is off topic here-- faq. If you explain what you are trying to accomplish there may be WordPress functions that will do it for you. Commented Jan 14, 2013 at 15:25
  • The first query im runing is a wordpress db query. Where im already running a working query. The secend "pure php query" is to show what query i want to run in the same way as the first, what i wanna do i to run them together. Commented Jan 15, 2013 at 8:59
  • If you are hoping to stack queries like SELECT * FROM tableA; SELECT * FROM tableB; I am pretty sure you can't. PHP/MySQL doesn't support it as far as I know, not on a LAMP stack anyway. I've heard rumors that you can do with if you have ASP.NET support. I don't work with that though. Commented Jan 15, 2013 at 14:27

2 Answers 2

1

If you are going to do this is SQL, use a subquery.

SELECT *,
  (SELECT meta_value 
    FROM wp_commentmeta 
    WHERE meta_key = 'your-meta-key' 
    AND wp_commentmeta.comment_id = wp_comments.comment_ID LIMIT 1) as comment_author 
FROM wp_comments

Instead of the *, enumerate the fields you want but leave out comment_author. Obviously, $wpdb functions to keep the table names straight.

0

Use the MySQL keyword NATURAL RIGHT JOIN

SELECT *  
  FROM table_name1 NATURAL RIGHT JOIN table_name2;

You can add WHERE, ORDER BY too

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.