简体   繁体   中英

Getting The Most Recent Entry From The Database

I have four tables for assigns , users , vehicles and fuel . I want to make a query that would return unique rows (that's to say, there should be just one unique ID for each of the tables except the users table).

I want the query to return the most recent entry, (like the last one of its kind).

My current query is below but it returns the first row it finds, not the last of its kind

Select distinct
     v.vname,
     v.year,
     v.make,
     v.model,
     v.vid,
     v.fueltype, 
     f.fID,
     f.meter,
     f.date,
     a.driverID,
     a.dateassigned,
     u.uid,
     u.name,
     u.surname
 from
     vehicles as v,
     assigns as a,
     users as u, 
     fuel as f
 where
     v.vid = a.vID
 and
     a.driverID = u.id
 and
     v.vid = f.vID
 group by
     v.vid
 order by
     f.date desc

How can I get the last entry from the database using these tables?

Try below code with limit :

   Select distinct
     v.vname,
     v.year,
     v.make,
     v.model,
     v.vid,
     v.fueltype, 
     f.fID,
     f.meter,
     f.date,
     a.driverID,
     a.dateassigned,
     u.uid,
     u.name,
     u.surname
 from
     vehicles as v,
     assigns as a,
     users as u, 
     fuel as f
 where
     v.vid = a.vID
 and
     a.driverID = u.id
 and
     v.vid = f.vID
 group by
     v.vid
 order by
     f.date desc 
 limit 1
    Select distinct     
      v.vname,     
      v.year,     
      v.make,     
      v.model,     
      v.vid,      
      v.fueltype,       
      f.fID,     
      f.meter,     
      f.date,      
      a.driverID,     
      a.dateassigned,     
      u.uid,     
      u.name,     
      u.surname from vehicles as v, 
      assigns as a, 
      users as u, 
      fuel as f where v.vid = a.vID and 
      a.driverID = u.id and v.vid = f.vID 
      group by v.vid  
      order by f.date desc 
      limit 1

Any problems, please leave a comment.

Select 
     v.vname,
     v.year,
     v.make,
     v.model,
     v.vid,
     v.fueltype, 
     f.fID,
     f.meter,
     f.date,
     a.driverID,
     a.dateassigned,
     u.uid,
     u.name,
     u.surname
 from
     vehicles as v,
     assigns as a,
     users as u, 
     fuel as f
 where
     v.vid = a.vID
 and
     a.driverID = u.id
 and
     v.vid = f.vID
 order by
     v.id desc 
 limit 1

If your table vehicles having id as primary key then above SQL will work.

I don't think it's what you want right now but I just want to throw it there for the next time you create a database. This thing is easily solved by laravel in it's approach. Just create two database fields created_at and updated_at .

Both fields are timestamps and created_at updates as you create a item whereas updated_at updates as one tries to update any field on database. This way you can get the latest records on the database wheather it was created or updated.

As there is slim to none chance of two timestamps colliding with each other you will almost always get one result. Also you can order the records this way according to timestamps of either created or updated.

I am speculating that what you want is something along the lines of the following:

SELECT
    v.vname,
    v.year,
    v.make,
    v.model,
    v.vid,
    v.fueltype, 
    f1.fID,
    f1.meter,
    f1.date,
    a.driverID,
    a.dateassigned,
    u.uid,
    u.name,
    u.surname
FROM vehicles v
INNER JOIN assigns a
    ON v.vid = a.vID
INNER JOIN users u
    ON a.driverID = u.id
INNER JOIN fuel f1
    ON v.vid = f1.vID
INNER JOIN
(
    SELECT vID, MAX(date) AS max_date
    FROM fuel
    GROUP BY vID
) f2
    ON f1.vID = f2.vID AND f1.date = f2.max_date
ORDER BY
    f1.date DESC;

This query just does an additional join to a subquery which finds, for each vehicle, the most recent date. This restricts your current query by removing any records which are not the most recent, for each vehicle.

Note that I converted your old school implicit joins to explicit inner joins. This is the preferred way of writing SQL queries these days.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM