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

-3

I took data from a wp_posts table and now I need to get data from the wp_postmeta table that matches < strong> ID tag captured in the wp_posts table. This data is the product_img1 , product_img2 , product_img3 > as shown in the image below:

TheSQLthatIusedtocapturethewp_postsinformationisthis:

<?phprequire("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?

    
asked by anonymous 31.10.2014 / 19:55

1 answer

3

You have not passed 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'
    
31.10.2014 / 20:11