mysql - 如何加入同一张 table 两次?

标签 mysql sql mysql-workbench

我有两个表: 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/

相关文章:

MySQL - 如果列已经被索引,主键是否会再创建一个索引?

MySQL断言

MYSQL - 'SELECT' 和 'AS 只能创建一个列..如何让它们更多?

mysql - 替换 mysql-href 中的字符串

mysql - 如何选择2个日期时间类型变量内同一天的案例

c# - SQL Server Integration Services (SSIS) 是否与 C# 有这种交互?

mysql - 组列与无组列的混合

MySQL: errno: 105 无法创建表

php - HTML/PHP 表单问题

sql - 如何传递变量内字符串的参数值?