0

I need to get values from a subquery inside a LEFT JOIN.

I will use here the example used in this answer: https://stackoverflow.com/a/16776190/17580361

SELECT wp_woocommerce_order_items.order_id As No_Commande
FROM  wp_woocommerce_order_items
LEFT JOIN 
    (
        SELECT meta_value As Prenom, post_id
        FROM wp_postmeta
        WHERE meta_key = '_shipping_first_name'
    ) AS a
ON wp_woocommerce_order_items.order_id = a.post_id
WHERE  wp_woocommerce_order_items.order_id =2198 

Now how can I select values from wp_postmeta table?

If you need more info, please tell me.

Thank you very much.

  • Just include them in your outer select. `SELECT wp_woocommerce_order_items.order_id As No_Commande, Prenom FROM ....`. Also I see no reason why you are using a nested select instead of a "normal" table join. Using a table join might get you a more efficient query plan as well. – Igor Feb 14 '22 at 18:37
  • Just add them to the output. – shawnt00 Feb 14 '22 at 19:55

1 Answers1

0
SELECT 
    wp_woocommerce_order_items.order_id As No_Commande,
    a.Prenom
FROM  wp_woocommerce_order_items
LEFT JOIN 
    (
        SELECT meta_value As Prenom, post_id
        FROM wp_postmeta
        WHERE meta_key = '_shipping_first_name'
    ) AS a
ON wp_woocommerce_order_items.order_id = a.post_id
WHERE  wp_woocommerce_order_items.order_id =2198 
Marcio Rocha
  • 186
  • 2
  • 9