LIMIT显示不同的表字段

时间:2013-11-29 08:13:42

标签: mysql sql

以下是我的表格:

1. tbluser
UserNumber - PK
Name    
MemberType - Number
StationNumber - FK (connected to StationNo of tblStation)

2.tblStation
StationNo - PK
StationName

3.tblUserLogs
LogID - PK
UserID - FK (connected from UserNumber of tblusers)
LastLog

我想要做的就是显示名称(tblusers),StationName(tblStation)和LastLog(tblUserLogs),其中MemberType不等于1.

这是我的尝试...

SELECT tblusers.FirstName, tblstation.StationName, tblUserLogs.LastLog
FROM (tblstation INNER JOIN tblusers ON tblstation.StationNo = tblusers.StationNumber) 
     INNER JOIN tblUserLogs ON tblusers.UserNumber = tblUserLogs.UserID
WHERE (((tblusers.MemberType)<>1))

但是,我会重复记录我的用户。它显示所有LastLog数据,而不是显示最新数据。

我该怎么做?

1 个答案:

答案 0 :(得分:1)

使用此查询:

SELECT tblusers.FirstName, tblstation.StationName, MAX(tblUserLogs.LastLog)
FROM (tblusers LEFT JOIN tblstation ON tblstation.StationNo = tblusers.StationNumber)
     LEFT JOIN tblUserLogs ON tblusers.UserNumber = tblUserLogs.UserID
WHERE tblusers.MemberType != 1