简体   繁体   English

加入 MySQL 中的 4 个表,其中一个表的最大行数

[英]Join 4 tables in MySQL with max row from one table

I have four tables in a MySQL database and I am trying to create a query that joins all four pulling the most recent data from the logs table:我在 MySQL 数据库中有四个表,我正在尝试创建一个查询来连接所有四个从日志表中提取最新数据的查询:

  • ratings (id, rating, deviceId, listingId)评级(id、评级、deviceId、listingId)
  • data (listingId, name, location)数据(listingId、名称、位置)
  • devices (deviceId, model)设备(设备 ID、型号)
  • logs (dateAdded, deviceId, appVersion)日志(添加日期、设备 ID、应用版本)

I would like to display: rating, name, location, model, appVersion我想显示:评级、名称、位置、model、appVersion

The application version change over time so it is kept separate from the devices table.应用程序版本随时间变化,因此它与设备表分开。

The following query gets me half way there.以下查询让我走到了一半。 The problem is the appVersion is not the most recent.问题是 appVersion 不是最新的。

SELECT ratings.id,ratings.listingId,ratings.rating,data.name,devices.model,tt.appVersion
FROM ratings
JOIN data on ratings.listingId = data.id 
JOIN devices on ratings.deviceIdentifier = devices.deviceIdentifier
JOIN (select max(dateAdded), appVersion, deviceIdentifier from logs) tt
ORDER BY dateAdded DESC;

If I try either of the following statements, the query times out.如果我尝试以下任一语句,查询就会超时。

SELECT ratings.id,ratings.listingId,ratings.rating,data.name,devices.model,tt.appVersion
FROM ratings
JOIN data on ratings.listingId = data.id 
JOIN devices on ratings.deviceIdentifier = devices.deviceIdentifier
JOIN (select max(dateAdded), appVersion, deviceIdentifier from logs where dateAdded in     
 (select max(dateAdded) from logs group by deviceIdentifier) ) tt
ORDER BY dateAdded DESC;

or或者

SELECT ratings.id,ratings.listingId,ratings.rating,data.name,devices.model,logs.appVersion
FROM ratings
JOIN data on ratings.listingId = data.id 
JOIN devices on ratings.deviceIdentifier = devices.deviceIdentifier
JOIN (select t.deviceIdentifier, t.appVersion 
    from logs t
    inner join (select deviceIdentifier, max(dateAdded) as dateAdded from logs  
            group by deviceIdentifier) x 
            on t.deviceIdentifier = x.deviceIdentifier and t.dateAdded = x.dateAdded) tt
ORDER BY dateAdded DESC;

EDITED: Corrected small bug.已编辑:更正了小错误。 I've tested this and it works.我已经对此进行了测试,并且可以正常工作。

set @deviceId := 0;
select *
from (select r.id, r.listingId, r.rating, data.name, d.model, d.deviceId, l.appVersion
    FROM ratings r
    JOIN data on r.listingId = data.listingId 
    JOIN devices d on r.deviceId = d.d  eviceId
    JOIN logs l on l.deviceId = d.deviceId
    ORDER BY d.deviceId, dateAdded DESC) x
where @deviceId != deviceId
and (@deviceId := deviceId) is not null;

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

相关问题 MySQL - 根据一个表的 MAX(聚合值)从 2 个表中检索一行 - MySQL - Retrieving a row from 2 tables based on MAX(aggregatedvalue) of one table MYSQL仅在具有来自多个表的数据的最大行上联接 - MYSQL Join only on max row with data from multiple tables MySQL内部连接两个表,并在匹配的行字段中将一个表的行插入另一个表 - MySQL Inner Join two tables and insert row from one table to other on matching row field 加入MySQL表:在左表的一行中显示右表的所有结果 - Join MySQL Tables: Display All Results From Right Table In One Row Of Left Table mysql使用内部join和max()将两个表插入一个表中 - mysql insert from two tables into one using inner join and max() 从mysql中的另一个表加入一行 - join one row from another table in mysql 当一个表丢失一行时,MySQL连接两个表 - MySQL join two tables when one table is missing a row 连接 MySQL 中的两个表,结果集中只有一行,从第二个表中选择了几列 - Join two tables in MySQL, have just one row in the result set with few columns selected from second table 在 MySQL 中连接两个表,从第二个表中只返回一行 - Join two tables in MySQL, returning just one row from the second table 使用Codeigniter将表中的一行与MySQL中其他两个表的多行联接 - Join one row from a table with multiple rows of two other tables in MySQL using Codeigniter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM