How to recover elements from another table in an SQL query?

Asked

Viewed 154 times

-3

I took data from a table wp_posts and now I need to get data from the table wp_postmeta which coincide with the ID captured in the table wp_posts. These data are the product_img1, product_img2, product_img3 as shown in the image below:

inserir a descrição da imagem aqui

The SQL I used to capture information from wp_posts is this:

<?php

    require("configs/conxao.php");
    $conectado = $pdo->query("SELECT * FROM wp_posts WHERE post_type = 'wpcproduct'");
    $conectado->execute();

    while($produtos = $conectado->fetch(PDO::FETCH_ASSOC)){

        echo $produtos["ID"]."<br>";                                
        echo $produtos["post_title"]."<br>";

    }

?>  

Can you help me finish this SQL?

  • http://imasters.com.br/artigo/240/sql-server/entendendo-joins/

  • http://answall.com/questions/6441/qual%C3%A9-a-difference%C3%A7a-entre-Inner-Join-e-outer-Join

  • 3

    I didn’t understand your edition, did you incorporate the answer in the question? With that, invalidated the answer that was given. If you have questions about what the answer suggests, use the comments below it. I will reverse the edit.

  • Could connect directly to WP and use native functions.

  • You can help me with this @Brasofilo?

  • 1

    And actually, the best thing is for you to tell us what problem you’re trying to solve, not just the problem you have with the code. So you can answer with precision.

Show 1 more comment

1 answer

3


You did not pass the layout of the tables, but I believe that both have a primary key, which will make the connection between both ( what we call Join ).

Your select would look something like this.

select p.*, pm.*  
from wp_posts p  
join wp_postmeta pm on p.post_id = pm.meta_id 
WHERE p.post_type = 'wpcproduct'
  • I pasted the other answer code here. You should always [Dit] your posts to fix errors, instead of posting ok again?

  • Hein @rcaratchuk can show me how to recover table data wp_postmeta within the while please?

  • Marcos, I’m DBA, not PHP developer.I’m trying to understand what your problem is , to contribute to the forum, please be clearer in your doubt.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.