my db is very simple:
CREATE TABLE Account (
accountId int NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
PRIMARY KEY (accountId)
);
CREATE TABLE Manager (
managerId int NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
PRIMARY KEY (managerId)
);
CREATE TABLE ManagerAccount(
id int not null auto_increment,
managerId int not null,
accountId int not null,
primary key(id),
foreign key(managerid) references Manager (ManagerID),
foreign key(accountId) references Account (AccountID)
);
Now, when i return an account object to the user I need to pull all the account associated with specific account
So I did something like:
select m.name
from manager m
inner join ManagerAccount ma on m.managerId = ma.id
inner join Account a on ma.id = a.accountId
where a.accountId = 1;
but this does not give me the answer I want, I only get one manager name and there are 3 managers associated with accountId 1...
you can see here:
inner join ManagerAccount ma on m.managerId = ma.id
inner join Account a on ma.id = a.accountId
should be
inner join ManagerAccount ma on m.managerId = ma.managerId
inner join Account a on ma.accountId = a.accountId
In your query, you have an foreign key relationship of ManagerAccount.accountId with Account (AccountID). But you have mapped the primary key of ManagerAccount table with Account (AccountID).
SELECT
m.name
FROM
Manager m
INNER JOIN
ManagerAccount ma ON m.managerId = ma.managerId
INNER JOIN
Account a ON ma.accountId = a.accountId
WHERE
a.accountId = 1;
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.