好的问题在这里
表 1:
Id1, Id2
表 2
Id, Username
现在 thisTable 1 Id1
和Id2
variables 是唯一的键Table 2
我想选择Table 1
所有值
Username1, Username2
那么我该如何编写这个查询呢?
完整示例
表格1
1,3
3,5
表 2
1,Furkan
3,Obama
5,USA
结果
Furkan, Obama
Obama, USA
谢谢
好的问题在这里
表 1:
Id1, Id2
表 2
Id, Username
现在 thisTable 1 Id1
和Id2
variables 是唯一的键Table 2
我想选择Table 1
所有值
Username1, Username2
那么我该如何编写这个查询呢?
完整示例
表格1
1,3
3,5
表 2
1,Furkan
3,Obama
5,USA
结果
Furkan, Obama
Obama, USA
谢谢
尝试这个
这是http://www.sqlfiddle.com/#!2/e10b7/2/0
CREATE TABLE table1
(
Id1 int primary key,
Id2 int
);
INSERT INTO table1
VALUES (1, 3),
(3, 5);
CREATE TABLE table2
(
Id int primary key,
Username varchar(255)
);
INSERT INTO table2
VALUES (1, 'Furkan'),
(3, 'Obama'),
(5, 'USA');
您的 SQL 查询
select
(select Username
from table2 as t2
where t2.Id = t1.Id1) as coloum1 ,
(select Username
from table2 as t2
where t2.Id = t1.Id2) as coloum2
from table1 as t1
SELECT table2_1.Username AS Username1, table2_2.Username AS Username2
FROM table1
JOIN table2 AS table2_1 ON table1.Id1 = table2_1.Id
JOIN table2 AS table2_2 ON table1.Id2 = table2_2.Id
请参阅SQL Fiddle以获取实时示例
SELECT a.Username AS name_1, b.Username AS name_2 FROM
t1 JOIN t2 AS a ON t1.Id1 = a.Id
JOIN t2 AS b ON t1.Id2 = b.Id