employee
id name
1 steve
2 rob
3 bell
position
position_id employee_id position
1 e1 manager
2 e2 seller
3 e3 director
的问题是,外键的主键不同的格式。 我怎样才能得到结果使用SQL查询?
name position
steve manager
rob seller
bell director
employee
id name
1 steve
2 rob
3 bell
position
position_id employee_id position
1 e1 manager
2 e2 seller
3 e3 director
的问题是,外键的主键不同的格式。 我怎样才能得到结果使用SQL查询?
name position
steve manager
rob seller
bell director
你是说你的employee_id在你的position表中总是有一个以“e”为前缀的吗?如果是这样,那么这应该工作使用CONCAT
:
select e.name, p.position
from employee e join position p
on p.employee_id = concat('e',e.id)
CONCAT
功能将帮助您隐式转换您的数字ID,并在前面加“E”来匹配位置表的EMPLOYEE_ID。然后你可以做一个散列连接来从两个表中获得结果。
SQL FIDDLE DEMO
select E.name,
P.position
from Employee E
inner join Positions P
on P.employee_id = concat('e',E.id)
是的,这是可能的。你有什么尝试? – Aaron