根据关系表值从另一个表中选择

时间:2016-07-31 16:53:47

标签: php mysql banking

我在MySQL中有三个表:

1) bank_accounts

- accounts_id (PRIMARY)
- accounts_account_number (UNIQUE)

2) bank_accounts_customers

- accounts_customers_id (PRIMARY)
- accounts_customers_account_id (INDEX)
- accounts_customers_customer_id (INDEX)

3) bank_customers

- customers_id (PRIMARY)
- customers_customer_number (UNIQUE)
- customers_title
- customers_first_name
- customers_middle_name
- customers_last_name

我需要将<帐号>存储在 bank_accounts 表中,并将客户编号存储在 bank_customer 表中。表 bank_accounts_customers 存储客户与其拥有的帐户之间的链接,以便多个客户可以共享一个帐户。数据库中的所有表都被编入索引,并使用外键链接它们。

我不确定 INNER JOIN JOIN 是否有用以及如何操作?

我附上了数据库的图像(不是100%完成)。 https://s32.postimg.org/ia56fgjth/Screen_Shot_2016_07_31_at_5_51_38_pm.png

我尝试的查询是:

SELECT `bank_accounts`.`accounts_account_number`, `bank_customers`.`customers_customer_number`
FROM `bank_accounts`, `bank_customers`
INNER JOIN `bank_accounts_customers`
ON bank_accounts_customers`.`accounts_customers_account_id` = `bank_accounts`.`accounts_id`

3 个答案:

答案 0 :(得分:0)

 SELECT COMM_AGENT_ID,ADP,CHANNEL,SUM_AMOUNT_TIER,SALES_MGR 
 FROM GSM G 
 ORDER BY COMM_AGENT_ID,adp,SUM_AMOUNT_TIER DESC

试试这个.. !!

答案 1 :(得分:0)

select 
ba.accounts_account_number, 
bc.customers_title,bc.customers_first_name, bc.customers_middle_name, bc.customers_last_name 
from bank_accounts ba inner join bank_accounts_customers bac 
on ba.accounts_id = bac.accounts_customers_account_id 
inner join bank_customers bc 
on bac.accounts_customers_customer_id = bc.customers_id

应该得到你想要的东西。

答案 2 :(得分:0)

试试这个

SELECT ba.accounts_account_number, bc.customers_customer_number 
FROM bank_accounts_customers bac
INNER JOIN bank_accounts ba on bac.accounts_customers_account_id = ba.accounts_id
INNER JOIN bank_customers bc on bac.accounts_customers_customer_id = bc.customers_id