简体   繁体   English

从数据库获取最新条目

[英]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). 我想查询返回唯一行的查询(也就是说,除了用户表外,每个表都应该只有一个唯一ID)。

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. 如果您的餐桌车使用ID作为主键,那么上面的SQL将起作用。

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. laravel可以轻松解决此问题。 Just create two database fields created_at and updated_at . 只需创建两个数据库字段created_atupdated_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. 当您创建项目时,这两个字段都是时间戳和created_at更新,而当您尝试更新数据库中的任何字段时, updated_at更新。 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. 最近,这是编写SQL查询的首选方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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