如何在同一张桌子上两次加入比赛?

时间:2019-04-26 06:44:50

标签: mysql sql mysql-workbench

我有两个表: 1.带有交易编号以及起点和终点的交易表 2.点名的桌子

我需要使用以下格式针对每个交易记录一个条目: 交易编号,起点,起点名称,终点,终点名称

select t.id, t.start_point, s.name, t.end_point, s1.name
from transaction t 
left join stand s on s.stand_id = t.start_point and s.state = 2
left join stand s1 on s.stand_id = t.end_point and s1.state = 2
where t.state = 2 and date(t.created_at) = curdate()-1
and t.start_point is not null and t.end_point is not null

我的resut计数应该是3660,但是我得到的是8462。我得到3660,而没有s1左连接。如何更改此查询?

2 个答案:

答案 0 :(得分:0)

您输入了错误的select e1AsBoss.empno as idBoss , e2AsEmployer.empno as idEmployer from employeedetails as e1AsBoss inner join employeedetails as e2AsEmployer on e1AsBoss.empno= e2AsEmployer.bossno group by e1AsBoss.empno, e2AsEmployer.empno order by e1AsBoss.empno, e2AsEmployer.empno -而不是s的{​​{1}} 尝试

s1

答案 1 :(得分:0)

s1.stand_id = t.end_point用于第二个加入条件

select t.id, t.start_point, s.name, t.end_point, s1.name
from transaction t 
left join stand s on s.stand_id = t.start_point and s.state = 2
left join stand s1 on s1.stand_id = t.end_point and s1.state = 2
where t.state = 2 and date(t.created_at) = curdate()-1
and t.start_point is not null and t.end_point is not null