MYSQL QUERY:从查询中选择DISTINCT IMEI

时间:2014-03-23 00:42:48

标签: mysql sql

我正在查询输出将从IMEI中获取最新数据而没有任何重复,我希望最新的值只能在输出中看到,并且将忽略具有相同IMEI的第二个后续值。 。

这是我的QUERY:

SELECT 
tblWeather.humidity, tblWeather.rainfall,
tblWeather.wDate, tblWeather.wTime,
tblStations.IMEI, tblStations.Station, 
tblWeather.IMEI, tblWeather.msgID,
tblWeather.tempture 
FROM tblStations 
INNER JOIN tblWeather ON tblStations.IMEI = tblWeather.IMEI 
ORDER BY msgID DESC LIMIT 0,22

这个的输出是:

{
"tblStations": [
    {
        "Station": "AWS09 - Lasang",
        "Date": "2014-03-19",
        "Time": "16:15:01",
        "temperature": "25.2",
        "humidity": "92.4",
        "rainfall": "4.42"
    },
    {
        "Station": "AWS0C - AdDU",
        "Date": "2014-03-19",
        "Time": "16:15:01",
        "temperature": "25.4",
        "humidity": "88.6",
        "rainfall": "6.2"
    },
    {
        "Station": "AWS0A - Marilog",
        "Date": "2014-03-19",
        "Time": "16:15:01",
        "temperature": "22.6",
        "humidity": "94.6",
        "rainfall": "1.42"
    },
    {
        "Station": "AWS12 - AdDU HS",
        "Date": "2014-03-19",
        "Time": "16:15:01",
        "temperature": "26",
        "humidity": "89.5",
        "rainfall": "4.37"
    },
    {
        "Station": "AWS0D - Lacuesta",
        "Date": "2014-03-19",
        "Time": "16:15:00",
        "temperature": "18.5",
        "humidity": "98.5",
        "rainfall": "7.99"
    },
    {
        "Station": "AWS04 - DNSC",
        "Date": "2014-03-19",
        "Time": "16:15:01",
        "temperature": "25.6",
        "humidity": "92.7",
        "rainfall": "2.69"
    },
    {
        "Station": "AWS14 - Catigan",
        "Date": "2014-03-19",
        "Time": "16:15:01",
        "temperature": "25.4",
        "humidity": "78.9",
        "rainfall": "0.3"
    },
    {
        "Station": "AWS10 - Tamugan",
        "Date": "2014-03-19",
        "Time": "16:15:01",
        "temperature": "25.4",
        "humidity": "89.9",
        "rainfall": "1.59"
    },
    {
        "Station": "AWS05 - Panacan",
        "Date": "2014-03-19",
        "Time": "16:15:01",
        "temperature": "25.7",
        "humidity": "92.9",
        "rainfall": "2.2"
    },
    {
        "Station": "AWS03 - UPMin",
        "Date": "2014-03-19",
        "Time": "16:15:01",
        "temperature": "25.9",
        "humidity": "83.2",
        "rainfall": "0"
    },
    {
        "Station": "AWS11 - Mudiang",
        "Date": "2014-03-19",
        "Time": "16:15:01",
        "temperature": "24.9",
        "humidity": "92.2",
        "rainfall": "1.66"
    },
    {
        "Station": "AWS07 - Unlad Kabayan",
        "Date": "2014-03-19",
        "Time": "16:15:01",
        "temperature": "26.5",
        "humidity": "82.6",
        "rainfall": "0.21"
    },
    {
        "Station": "AWS13 - Calinan",
        "Date": "2014-03-19",
        "Time": "16:15:01",
        "temperature": "26.5",
        "humidity": "83.9",
        "rainfall": "0"
    },
    {
        "Station": "AWS01 - USeP",
        "Date": "2014-03-19",
        "Time": "16:15:01",
        "temperature": "26.9",
        "humidity": "86.4",
        "rainfall": "1.63"
    },
    {
        "Station": "AWS11 - Mudiang",
        "Date": "2014-03-19",
        "Time": "16:00:01",
        "temperature": "25.1",
        "humidity": "92.2",
        "rainfall": "1.62"
    },
    {
        "Station": "AWS0C - AdDU",
        "Date": "2014-03-19",
        "Time": "16:00:01",
        "temperature": "25.3",
        "humidity": "89.9",
        "rainfall": "6.2"
    },
    {
        "Station": "AWS0D - Lacuesta",
        "Date": "2014-03-19",
        "Time": "16:00:00",
        "temperature": "18.5",
        "humidity": "98.4",
        "rainfall": "7.79"
    },
    {
        "Station": "AWS03 - UPMin",
        "Date": "2014-03-19",
        "Time": "16:00:01",
        "temperature": "25.7",
        "humidity": "83.1",
        "rainfall": "0"
    },
    {
        "Station": "AWS10 - Tamugan",
        "Date": "2014-03-19",
        "Time": "16:00:01",
        "temperature": "25.3",
        "humidity": "90",
        "rainfall": "1.58"
    },
    {
        "Station": "AWS04 - DNSC",
        "Date": "2014-03-19",
        "Time": "16:00:01",
        "temperature": "25.9",
        "humidity": "92",
        "rainfall": "2.66"
    },
    {
        "Station": "AWS05 - Panacan",
        "Date": "2014-03-19",
        "Time": "16:00:01",
        "temperature": "25.7",
        "humidity": "92.8",
        "rainfall": "2.18"
    },
    {
        "Station": "AWS13 - Calinan",
        "Date": "2014-03-19",
        "Time": "16:00:01",
        "temperature": "26.7",
        "humidity": "84.5",
        "rainfall": "0"
    }
],

我想得到的只是从每个电台获取一个数据,而不是从每个电台获取任何重复数据

电台IMEI是每个电台的唯一值。

你可以帮我吗...非常感谢你/..// p>

我尝试进行不同的查询,但我似乎没有从查询中获取任何值..

2 个答案:

答案 0 :(得分:0)

您似乎希望从每个电台获取最新消息ID的消息。问题是在SQL中没有超级简单的方法来做这种事情。您需要将数据拉入临时表(明确或隐含地使用子查询),以便您可以将每个站的最新ID加入天气数据。

这样的过程适合你:

CREATE TEMPORARY TABLE latest_messages 
SELECT MAX(msgID, IMEI) FROM tblWeather 
GROUP BY IMEI;

将它放入显式临时表的好处是,您可以将该索引编入索引以供以后加入,这种技术只有在latest_messages变得非常大时才真正重要。加入子查询可能就足够了。如果数据对于这两种技术来说都太大了,那么每当有更新时,您可能需要触发器或手动方法来更新latest_messages

然后,一旦您收到来自每个电台的最新消息,您就可以将该临时表加入tblWeather,以查找每个电台的最新天气数据。

如果您不想或不想创建临时表,您可以尝试这样的事情:

  select * from  (
    SELECT MAX(msgID, IMEI) newest_id FROM tblWeather 
    GROUP BY IMEI
   ) AS most_recent_messages 
   JOIN tblWeather on newest_id = tblWeather.msgID
   JOIN tblStations on tblStations.IMEI = tblWeather.IMEI;

希望它有所帮助!如果需要更具体的示例,请创建SQLFiddle。我厌倦了重建人们的模式:)

答案 1 :(得分:0)

您可以先按日期对条目进行排序,然后按任何{station_identifier}进行分组 - 瞧,每个电台都有最新的条目:

SELECT * FROM (SELECT * FROM entries ORDER BY c_time DESC) as temp  GROUP BY station;

请参阅小提琴:http://sqlfiddle.com/#!2/b8a28/1

也许我的评论有点不清楚:ofc。在一个不起作用的查询中(没有最小/最大)。但是你不必使用Min / Max或其他任何东西来获得每个“分组单元”的最新条目。

Sitenote:在MsSQL上这不起作用:如果没有在其他列上定义聚合(由我们之前完成的排序定义),Mysql将选择“第一”结果,而不会在其他列上定义聚合--MsSQL将抛出错误。


说明:

假设行

foreign_id | date | msg
1 | 2014-03-23 00:00:00 | Newest Message
1 | 2014-03-22 00:00:00 | Older Message

Mysql会按照给定的方式对它们进行排序。最后,由于分组,它将按ID进行分组,并选择第一行(每个组的第一行是我们需要的 - 由于排序):2014-03-23 00:00:00Newest Message作为结果。< / p> 在MsSQL上,您需要提供MAX(date)然后清楚地定义1 | 2014-03-23 00:00:00 - 但是,MsSQL不知道如何处理msg列:它应该选择Newest Message还是Older Message?那是因为THAT列上的聚合丢失了,MsSQL没有对此进行隐式排序,因为BOTH值与Group By条件id=1匹配。