Home > Mobile >  SQL Join between two tables excluding some fields
SQL Join between two tables excluding some fields

Time:04-27

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 .

CodePudding user response:

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.

  • Related