MySQL LEFT JOIN 2列

时间:2016-05-17 12:43:50

标签: mysql

SELECT * FROM timetable 
LEFT JOIN user 
ON timetable.id = user.id
WHERE timetable.id = 1
//this only display 1 column's name from user table
//id  partner name  time
//1   2       Ben   2015-05-17

//user
id  name
1   Ben
2   Lisa

//timetable
id  partner  time
1   2        123456484

//What I need is display like this
Ben  Lisa    2015-05-17

我有2个表用户和时间表

用户表存储ID和名称

合作伙伴组合在一起的时间表商店ID

现在,我需要将这两个表连接在一起并显示其名称

我尝试加入,但它只显示一个列的名称

任何人都知道如何显示2列?

1 个答案:

答案 0 :(得分:0)

你的解释不明确。但这是我的猜测:

SELECT 
  u.name,
  p.name,
  t.time 
FROM timetable t
LEFT JOIN user u
ON timetable.id = u.id
LEFT JOIN user p
ON timetable.partner = p.id
WHERE timetable.id = 1