[英]how to query data from multiple tables and display one record specific to each device relative to their last working date
Hi I am new to mysql database can any one help to my problem I have three tables like this 嗨,我是mysql数据库的新手,可以帮我解决我的问题吗?我有3张这样的表
select * from device;
Vehiclegroup vehicleName deviceid accountid
------------ ----------- -------- -----------
sms cargo test1 sysadmin
sms cargo1 test2 sysadmin
sms cargo3 test3 sysadmin
select * from Account;
accountid password deviceid
--------- -------- ----------
sysadmin pass test3
sysadmin pass test2
sysadmin pass test1
And finally
mysql> select * from eventdata;
+-----------+-------+----------+----------+
| accountid | speed | date | deviceid |
+-----------+-------+----------+----------+
| sysadmin | 45 | 12/10/13 | test3 |
| sysadmin | 45 | 10/10/13 | test3 |
| sysadmin | 45 | 10/10/13 | test2 |
| sysadmin | 45 | 11/10/13 | test2 |
| sysadmin | 45 | 14/10/13 | test2 |
| sysadmin | 45 | 17/10/13 | test2 |
| sysadmin | 45 | 17/10/13 | test1 |
+-----------+-------+----------+----------+
7 rows in set (0.00 sec)
here i need output table as shown below 这里我需要输出表,如下所示
vehiclegroup vehiclename deviceid date
sms cargo1 test1 17/10/13
sms cargo2 test2 17/10/13
sms cargo3 test3 12/10/13
ie i need data for each device for the last working date can any one help for this 即我需要每个设备上次工作日期的数据,对此有何帮助?
Something like this? 像这样吗
SELECT d.Vehiclegroup, d.vehicleName, e.deviceid, e.date
FROM eventdata e
INNER JOIN device d ON e.deviceid = d.deviceid
GROUP BY e.deviceid
Will give you one record per deviceid. 每个设备ID都会给您一条记录。 The Vehiclegroup, vehiclename and date are chosen arbitrary from the list.
车辆组,车辆名称和日期可从列表中任意选择。 If you want the latest, you should use a subquery or modifier, like ( MAX(e.date) as date)
如果需要最新版本,则应使用子查询或修饰符,例如(以MAX(e.date)作为日期)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.