简体   繁体   中英

SQL join left and right

This is my query:

select distinct * 
from purchase_records a 
inner join purchase_items b on a.id = b.purchase_id 
left join transactions c on a.id= c.purchase_id

It fetches records from two tables. From the left it gets one record, and from the right it gets the number of records which are more then one.

The issue is that when the table returns the record, it matches the exact record but repeat right side of the row according to the left side of row.

How can i get one record in the left and many records in the right? I want my left join to show one record, and right to show as many records as there are in the database.

数据库中的联接表

you can try like as....

    select A.*,B.add1,C.add2 from(
    select 1 id,'A' name union
    select 2 ,'B'  union
    select 3 ,'C' union
    select 4 ,'D') A
    left join 
    (select 1 id,'A' name,'z' add1 union
    select 5 ,'B' ,'zz'  union
    select 6 ,'C' ,'zzz' union
    select 4 ,'D' ,'zzzz')B on A.id=B.id
    right join
    (select 1 id,'A' name,'1z' add2 union
    select 5 ,'B' ,'1zz'  union
    select 6 ,'C' ,'1zzz' union
    select 4 ,'D' ,'1zzzz')C on C.id=A.id

or like..

select A.columns,B.columns,C.columns from your_table1 A
left join your_table2 B on A.id=B.id
right join your_table3 C on C.id=A.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