我有两张表如下:
- tblEmployee
employeeID | Name
10 | sothorn
20 | lyhong
30 | sodaly
40 | chantra
50 | sangha
60 | bruno
- tblSale
ID | employeeID | employeeSaleID
1 | 30 | 10
2 | 10 | 40
3 | 50 | 20
我想从tableSale中选择并使用tblEmployee结果加入:
1 | sodaly | sothorn
2 | sothorn | chantra
3 | sangha | lyhong
答案 0 :(得分:3)
以下是您的数据的示例查询。 http://sqlfiddle.com/#!2/b74ca/5/0
答案 1 :(得分:2)
只需选择tblSale
表的所有行,然后加入tblEmployee
表两次:
SELECT s.ID, e1.Name, e2.Name
FROM tblSale s
INNER JOIN tblEmployee e1
ON e1.employeeID = s.employeeID
INNER JOIN tblEmployee e2
ON e2.employeeID = s.employeeSaleID
答案 2 :(得分:2)
试试这个:
在这里,您需要使用内部联接从两个表中获取数据。
select
id, e1.name as name1, e2.name as name2
from
tblSale s, tblEmployee e1, tblEmployee e2
where
s.employeeID=e1.employeeID
and
s.employeeSaleID=e2.employeeID
order by
s.id
答案 3 :(得分:2)
这很简单
QUERY
SELECT
tblSale.ID,
l.Name,
r.Name
FROM tblSale
INNER JOIN tblEmployee l On l.employeeID = tblSale.employeeID
INNER JOIN tblEmployee r ON r.employeeID = tblSale.employeeSaleID
输出
| ID | LNAME | RNAME |
|----|---------|---------|
| 1 | sodaly | sothorn |
| 2 | sothorn | chantra |
| 3 | sangha | lyhong |
答案 4 :(得分:1)
只需加入员工表两次,一次加入employeeID,一次加入employeeSaleID