I have two tables linked by UID
table A
1 A1
2 B3
3 E9
4 F2
table B
1 2
2 4
3 1
I want to create a view based on table B with A's UID in it.
View_link
1 2 A1 B3
2 4 B3 F2
3 1 E9 A1
The view I tried as below.
ALTER VIEW view_links AS
SELECT
tableB.source as sourceID,
tableB.target as targetID,
tabelA.UID as sourceUID
FROM tableA, tableB
where tableB.source=tableA.id
How can I add targetUID into the view?
Grab the link in a sub query and join back to table a (and don't use implicit joins)
for example
drop table if exists ta,tb;
create table ta(id int, uid varchar(2));
create table tb(id int, uid varchar(2));
insert into ta values
(1, 'A1'),
(2, 'B3'),
(3, 'E9'),
(4, 'F2');
insert into tb values
(1 ,2),
(2 ,4),
(3 ,1);
select taid,tbuid,tauid,ta.uid
from
(
select ta.id taid,ta.uid tauid,tb.uid tbuid
from ta
join tb on tb.id = ta.id
) a
join ta on ta.id = tbuid
order by taid
result
+------+-------+-------+------+
| taid | tbuid | tauid | uid |
+------+-------+-------+------+
| 1 | 2 | A1 | B3 |
| 2 | 4 | B3 | F2 |
| 3 | 1 | E9 | A1 |
+------+-------+-------+------+
3 rows in set (0.00 sec)
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.