是否可以在没有别名的情况下编写此sql查询?我正在使用一个不包含别名的PHP脚本,所以我遇到了问题。
如果可以,请向我提供一些帮助
这是代码:
SELECT
time1.Time, time2.Time, time1.Signal, v.name, v.lastname, k.vehicle, time1.Reg
FROM
data time1
INNER JOIN data time2
ON time1.id != time2.id
AND time1.serial= time2.serial
INNER JOIN drivers v
ON time1.FK_ID_driver=v.ID_driver
INNER JOIN vehicles k
ON time1.Reg=k.Reg
WHERE
TIMEDIFF(time2.Time, time1.Time) BETWEEN '00:15:00' AND '00:30:00';
答案 0 :(得分:2)
你不能轻易实现你想要的,因为你要加入同一个表两次,而SQL需要一个别名来消除歧义。
但是,您可以为表data
创建一个视图,并在其中一个data
联接中使用视图而不是表名。
示例:强>
select data.time,
vData.time,
data.Signal,
drivers.name,
drivers.lastname,
vehicles.vehicle,
data.Reg
from data
inner join vData on data.id != vData.id and data.serial = vData.serial
inner join drivers on data.FK_ID_driver = drivers.ID_driver
inner join vehicles on data.Reg = vehicles.Reg
where TIMEDIFF(vData.time, data.time) between '00:15:00' and '00:30:00';