简体   繁体   中英

SQL Join between two tables excluding some fields

I have two tables Customer and Beneficiary, the relation between them is ManyToMany, the generated table customers_beneficiaries contains the Id of Beneficiary and the Id of Customer i want to get the list of customers with a given beneficiary_id

SELECT * from customer c 
Full OUTER JOIN customers_beneficiaries cb 
ON c.id= cb.customer_id
WHERE cb.beneficiary_id=8;

But the result iam getting contains the two fields of customers_beneficiaries table (customer_id && beneficiary_id) How can i exclude them from the result Thank you.

Try this:(In case you can change id column name in customer table to customer_id )

SELECT c.* from customer c 
Full OUTER JOIN customers_beneficiaries cb 
USING(customer_id)
WHERE cb.beneficiary_id=8;

USING Clause is like ON Clause which takes list of columns on which joining of table has to be done but those columns have to exist in both tables. The columns used in join operation appears only once in output.

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