简体   繁体   中英

SQL LEFT JOIN Subquery Alias

I'm running this SQL query:

SELECT wp_woocommerce_order_items.order_id As No_Commande
FROM  wp_woocommerce_order_items
LEFT JOIN 
    (
        SELECT meta_value As Prenom
        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

And I get this error:

#1054 - Unknown column 'a.post_id' in 'on clause'.

I think my code is pretty simple, but I can't make it right. What am I doing wrong?

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_Commande
FROM  wp_woocommerce_order_items
LEFT JOIN 
    (
        SELECT meta_value As Prenom, post_id  -- <----- this
        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 

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_Commande
FROM  wp_woocommerce_order_items AS wpoi
LEFT JOIN wp_postmeta AS wpp ON wpoi.order_id = wpp.post_id 
                            AND wpp.meta_key = '_shipping_first_name'
WHERE  wpoi.order_id =2198 

create database Joins_With_F_K

use Joins_With_F_K

create table person( id int identity primary key not null, Name varchar(20), address varchar(20), contact int, city varchar(20), food_id int, )

create table Order1 ( id int identity primary key not null, food varchar(20) not null, date varchar(20) not null, quantity int not null, flavour varchar(20) not null, amount int not null

                     )

alter table person add constraint person_fk foreign key (food_id) references order1(id)

insert into person values ('marium','nazimabad',033513,'karachi',1), ('faiza','nazimabad',033513,'karachi',2), ('anabia','nazimabad',033513,'karachi',3), ('aqsa','nazimabad',033513,'karachi',4), ('savera','nazimabad',033513,'karachi',5), ('rubi','nazimabad',033513,'karachi',6), ('bisma','nazimabad',033513,'karachi',7) insert into person values ('sami khan ','nazimabad',033513,'karachi',8)

insert into Order1 values('pizza','18\\01\\2022',1,'tikka',300), ('biryani','18\\01\\2022',1,'chicken tikka',200), ('juice','18\\01\\2022',1,'orange',150), ('burger','18\\01\\2022',1,'tikka',250), ('broast','18\\01\\2022',1,'chicken masala',300), ('pizza','18\\01\\2022',1,'fagita',300), ('pizza','18\\01\\2022',1,'malai boti',500)

insert into Order1 values('Zinger Pizza','18\\01\\2022',2,'tikka',300)

select * from person select * from order1

select * ,quantity*amount as t_aomunt from person as a inner join order1 as b on a.food_id = b. id

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM