0

是否可以在没有别名的情况下编写此 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';
4

1 回答 1

2

您无法轻松实现您想要的,因为您要加入同一个表两次,并且 SQL 需要一个别名来消除它们的歧义。

但是,您可以为 table 创建一个视图,并在其中一个联接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';
于 2012-10-04T20:39:51.460 回答