根据关系 table 的值从另一个 table 中选择
Selecting from another table based on relation tables value
我在 MySQL 中有三个 table:
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 table 中的帐号和存储在 [=52 中的客户编号=]table。 table bank_accounts_customers 在客户和他们拥有的帐户之间存储了一个 link,因此多个客户可以共享一个帐户。数据库中的所有 table 都已编入索引并使用外键 link 它们。
我不确定 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`
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
试试这个..!!
SELECT ba.accounts_account_number, bc.customers_customer_number
FROM bank_accounts AS ba
INNER JOIN bank_accounts_customers AS bac
ON bac.accounts_customers_account_id = ba.accounts_id
INNER JOIN bank_customers AS bc
ON bc.customers_id = bac.accounts_customers_customer_id
应有尽有
试试这个
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
我在 MySQL 中有三个 table:
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 table 中的帐号和存储在 [=52 中的客户编号=]table。 table bank_accounts_customers 在客户和他们拥有的帐户之间存储了一个 link,因此多个客户可以共享一个帐户。数据库中的所有 table 都已编入索引并使用外键 link 它们。
我不确定 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`
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
试试这个..!!
SELECT ba.accounts_account_number, bc.customers_customer_number
FROM bank_accounts AS ba
INNER JOIN bank_accounts_customers AS bac
ON bac.accounts_customers_account_id = ba.accounts_id
INNER JOIN bank_customers AS bc
ON bc.customers_id = bac.accounts_customers_customer_id
应有尽有
试试这个
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