How to left join meta in queries
I have a query something like
//read products with thumbnail and sku
SELECT products.*, pm1.meta_value as sku, p1.guid as thumbnail
FROM `wp_posts` as products
LEFT JOIN `wp_postmeta` as pm1 ON products.ID = pm1.post_id
LEFT JOIN `wp_postmeta` as pm2 ON products.ID = pm2.post_id
LEFT JOIN `wp_posts` as p1 ON pm2.meta_value = p1.ID
WHERE products.post_type = 'product' AND pm1.meta_key = '_sku' AND pm2.meta_key = '_thumbnail_id';
This Query will get all the products that have a sku and a thumbnail but will not get any products who are missing a sku or thumbnail.
How do I get all products and the sku if it exists and the thumbnail if it exists?
I do not want to have to run 3 queries and merge the data.