我一直试图找出在我的查询中添加其他变量的位置,以便正确地提取它,但我遇到了一些问题。这是我目前的查询:
SELECT
tt.unit_id,
tt.latitude,
tt.longitude,
tt.gps_settings,
tt.timestamp
FROM AA55 tt
LEFT JOIN device_link AS dl ON dl.device_id = unit_id
INNER JOIN (SELECT unit_id, MAX(timestamp) AS MaxTimestamp FROM AA55 GROUP
BY unit_id) AS groupedtt
ON tt.unit_id = groupedtt.unit_id
AND tt.timestamp = groupedtt. MaxTimestamp
WHERE dl.user_id = '$real_id';
基本上,我需要从名为nickname
的表中添加一个名为device_info
的列。我已经尝试在tt.unit_id
之后添加值,但我一直收到错误
Unknown column 'unit_id' in 'on clause'
需要通过将device_info.device_id
与AA55.unit_id
进行比较来获取昵称。
有没有办法做到这一点?提前谢谢。
答案 0 :(得分:0)
这应该有效:
SELECT
tt.unit_id,
d2.nickname,
tt.latitude,
tt.longitude,
tt.gps_settings,
tt.timestamp
FROM AA55 AS tt
LEFT JOIN device_link AS dl ON(tt.unit_id=dl.device_id)
LEFT JOIN device_info AS d2 ON(tt.unit_id=d2.device_id)
INNER JOIN (SELECT unit_id, MAX(timestamp) AS MaxTimestamp FROM AA55
GROUP BY unit_id) AS groupedtt
ON(tt.unit_id = groupedtt.unit_id)
AND tt.timestamp = groupedtt. MaxTimestamp
WHERE dl.user_id = '$real_id';