简体   繁体   中英

How to select from 2 FB SQL tables with foreign keys?

I have 2 tables created like the following:

create table accounts(
accountOID int primary key not null,
bankcodenumber int,
accountnumber int
);

create table user(
userOID int primary key not null,
name varchar(25),
accountOID0 int,
accountOID1 int
);

Each user has 1 or 2 accounts, so to declare the foreign keys I type this:

alter table user add foreign key(accountOID0) references account(accountOID);

alter table user add foreign key(accountOID1) references account(accountOID);

Now, how can I list the accounts like this:

Name    | Bankcodenumber | Accountnumber
Sanchez | 0123456789     | 547
Trent   | 1234567890     | 278
Sanchez | 2345678901     | 480
Carlson | 3435577890     | 503
Jackson | 6833556904     | 168
...     | ...            | ...

I have tried this but with no success:

select name, (select bankcodenumber, accountnumber from account
where user.accountOID0 = accountOID or user.accountOID1 = accountOID)
from user order by name;

You seem to want union all :

select u.name, a.Bankcodenumber, a.Accountnumber
from user u join
     account a
     on u.accountoid0 = a.accountoid
union all
select u.name, a.Bankcodenumber, a.Accountnumber
from user u join
     account a
     on u.accountoid1 = a.accountoid;
SELECT u.name,
       a.bankcodenumber,
       a.accountnumber 
  FROM user u
INNER JOIN 
       accounts a
    ON u.accountOID0 = a.accountOID
UNION
SELECT u.name,
       a.bankcodenumber,
       a.accountnumber 
  FROM user u
INNER JOIN 
       accounts a
    ON u.accountOID1 = a.accountOID; 

An option without UNION but using a single SELECT with joins:

http://sqlfiddle.com/#!5/17ccc/1/0

select
   Coalesce(u1.name, u0.name) as name1,
   a.bankcodenumber,
   a.accountnumber
 from accounts a
   left join users u0
     on u0.accountOID0 = a.accountOID
   left join users u1
     on u1.accountOID1 = a.accountOID
 where name1 is not null

It uses the topicstarter's example data entered as

create table accounts(
accountOID int primary key not null,
bankcodenumber int,
accountnumber int
);

create table users(
userOID int primary key not null,
name varchar(25),
accountOID0 int references accounts(accountOID),
accountOID1 int references accounts(accountOID)
);

insert into accounts values (1, 0123456789, 547);
insert into accounts values (2, 1234567890, 278);
insert into accounts values (3, 2345678901, 480);
insert into accounts values (4, 3435577890, 503);
insert into accounts values (5, 6833556904, 168);

insert into users values(1, 'Trent', NULL, 2);
insert into users values(2, 'Carlson', 4, NULL);
insert into users values(3, 'Sanchez', 3, 1);
insert into users values(4, 'Jackson', 5, NULL);

It might loose some data if there are cases when one user's accountOID0 is equal to another user's accountOID1 . But the topicstarter implies there should be no such cases. Though he also does not say if there are some constraints or other guards to ensure that would never happen.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM