我有两个表: 1.交易表,有交易id和起止点 2. 点名站台
我需要针对每笔交易的单一条目,格式如下: 事务id,起点,起点名称,终点,终点名称
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
我的结果计数应该是 3660,但我得到的是 8462。我得到 3660 没有 s1 左连接。如何更改此查询?
最佳答案
你有一个错误的 s
- 而不是 s1
at left join stand s1 on s1.stand_id = t.end_point and s1.state = 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 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
关于mysql - 如何加入同一张 table 两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55862163/