SQL LEFT JOIN Subquery Alias SQL LEFT JOIN Subquery Alias sql sql

SQL LEFT JOIN Subquery Alias


You didn't select post_id in the subquery. You have to select it in the subquery like this:

SELECT wp_woocommerce_order_items.order_id As No_CommandeFROM  wp_woocommerce_order_itemsLEFT JOIN     (        SELECT meta_value As Prenom, post_id  -- <----- this        FROM wp_postmeta        WHERE meta_key = '_shipping_first_name'    ) AS aON wp_woocommerce_order_items.order_id = a.post_idWHERE  wp_woocommerce_order_items.order_id =2198 


I recognize that the answer works and has been accepted but there is a much cleaner way to write that query. Tested on mysql and postgres.

SELECT wpoi.order_id As No_CommandeFROM  wp_woocommerce_order_items AS wpoiLEFT JOIN wp_postmeta AS wpp ON wpoi.order_id = wpp.post_id                             AND wpp.meta_key = '_shipping_first_name'WHERE  wpoi.order_id =2198