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

Sorry my english !!!!

I get data from a table wp_posts and now I need to get data from the table wp_postmeta that match the ID captured in wp_posts table.

Read More

The data that I need to grab in the table wp_postmeta are product_img1, product_img2, product_img3 as shown in image below.

enter image description here

This is a view of my wp_posts:

enter image description here

I need to show ID, post_title, product_img1, product_img2, product_img3 and I am the SQL:

$show_info = $pdo("SELECT p.*, pm.*  
                   FROM wp_posts p  
                   JOIN wp_postmeta pm ON p.post_id = pm.meta_id 
                   WHERE p.post_type = 'wpcproduct'"
                 );

Can you help me to show this data for ID 103?

Related posts

Leave a Reply

1 comment

  1. I think the correct query for you is:

    SELECT p.*, pm.*  
    FROM wp_posts p  
    JOIN wp_postmeta pm ON p.ID = pm.post_id 
    WHERE p.post_type = 'wpcproduct'
    

    Since you want to match the post_id column on the wp_postmeta with the ID column on wp_posts

    If you only want data from post id 103, do:

    SELECT p.*, pm.*  
    FROM wp_posts p  
    JOIN wp_postmeta pm ON p.ID = pm.post_id 
    WHERE p.post_type = 'wpcproduct'
    AND p.ID = 103
    

    If you only want to display some columns, then do the following:

    SELECT p.ID, p.post_title, pm.product_img1, pm.product_img2, pm.product_img3 
    FROM wp_posts p  
    JOIN wp_postmeta pm ON p.ID = pm.post_id 
    WHERE p.post_type = 'wpcproduct'
    AND p.ID = 103