我有 2 张桌子。一张表显示位置信息,另一张表显示该位置的 wifi ssid 和密码。这些表由“LocationID”列连接。使用 2 个单独的表的原因是,我可以保留特定位置的 ssid 和密码的历史记录。
我的查询如下:
SELECT Location.LocationID,
Location.SystemName,
Location.SiteNameLocation,
Location.SiteAddress1,
Location.SiteAddress2,
Location.SiteCity,
Location.SiteProvince,
Location.SitePostalCode,
Location.ContactName,
Location.ContactPhone,
Location.ContactEmail,
Location.ID1POC,
Location.ID2District,
Location.SiteLocationHours,
LocationWiFi.WiFiSSID,
LocationWiFi.WiFiPassphrase,
LocationWiFi.WiFiDate
FROM Location
LEFT JOIN LocationWiFi ON LocationWiFi.LocationID = Location.LocationID
我需要做的是从表中获取最新的 wifi ssid 和密码以显示它与相应的 locationID
我可以使用以下查询来做到这一点:
SELECT * FROM LocationWiFi WHERE WiFiDate = (SELECT MAX(WiFiDate) FROM LocationWiFi) AND LocationID = xxxx
我相信这并不难,我只是想不通。提前感谢您的帮助!
编辑...
很抱歉给大家带来了困惑。让我尝试更清楚。我在位置表中有 1500 多个条目。LocationWiFi 表中有 2 个具有相同 locationid 的条目。我需要第一个查询(带有连接的那个)来返回所有 1500+ 行,但只使用最大日期提取 wifi ssid 和密码。对于没有 wifi ssid 和密码短语的位置,我只想显示具有空值的列。
我再次道歉
解决方案
嗨,大家好,
我已经解决了这个问题。对于任何有类似解决方案的人来说,这对我有用。
SELECT Location.LocationID,
Location.SystemName,
Location.SiteNameLocation,
Location.SiteAddress1,
Location.SiteAddress2,
Location.SiteCity,
Location.SiteProvince,
Location.SitePostalCode,
Location.ContactName,
Location.ContactPhone,
Location.ContactEmail,
Location.ID1POC,
Location.ID2District,
Location.SiteLocationHours,
a.WiFiSSID,
a.WiFiPassphrase
FROM Location
LEFT JOIN(
SELECT LocationWiFi.WiFiSSID,
LocationWiFi.WiFiPassphrase,
LocationWiFi.LocationID
FROM LocationWiFi
WHERE LocationWiFi.WiFiDate = (SELECT MAX(WiFiDate) FROM LocationWiFi)
) AS a
ON a.LocationID = Location.LocationID;