0

Is it possible to write this sql query without alias? I am using a PHP script that doesn't covers alias so I have problem with that.

If this is possible please provide me with some help

This is the code:

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';
1
  • "doesn't cover alias"? As in you can't specify which column you'd like to get back? Yow. Commented Oct 4, 2012 at 20:46

1 Answer 1

2

You can't easily achieve what you want, since you are joining the same table twice, and SQL needs an alias to disambiguate them.

You could, however, create a view for table data, and use the view instead of the table name in one of the data joins.

Example:

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';
Sign up to request clarification or add additional context in comments.

3 Comments

can you give me the example? I will always forward different values in the query
so how to create view? or this is whole example? I'm sorry but I haven't done views yet
@denonth See the docs: CREATE VIEW Syntax

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.