S Elizabeth Starkey
S Elizabeth Starkey

Reputation: 55

How to Perform a Join in MySQL

I am trying to print a list of accounts that are in 2 branches. Data is coming from 2 tables: account & branch. How do I get the db to print the customer id, account number and account type from the account table and branch name from the branch table? I figure there needs to be a JOIN command somewhere.

Upvotes: 0

Views: 82

Answers (2)

Benjamin
Benjamin

Reputation: 749

I don't know your field names, but you'd need something like this if you have branch_id on account:

SELECT account.cust_id, account.account_id, account.product_cd, branch.name INNER JOIN branch ON branch.id = account.branch_id FROM account

Or if you have account_id on branch:

SELECT account.cust_id, account.account_id, account.product_cd, branch.name INNER JOIN account ON branch.account_id = account.id FROM branch

Upvotes: 1

Tom
Tom

Reputation: 6663

This will work if assuming you have a field named "branch_id" on both tables. Otherwise insert the linking field names.

SELECT  a.`customer id`, a.`account number`, a.`account type`, b.`branch name`

FROM    account AS a

        JOIN branch AS b
        ON a.branch_id = b.branch_id

You can remove the backticks (`) if your field names do not have spaces.

Upvotes: 0

Related Questions