基于行值

时间:2017-10-23 23:35:02

标签: mysql sql

我有一个包含两种不同GPS设备类型的数据库。我需要创建一个基于GPS设备类型进行过滤的查询。

(gl200)设备类型查询如下所示:

SELECT t1.deviceid, t1.devicetime, t1.latitude, t1.longitude
FROM positions t1
INNER JOIN
    (SELECT MAX(id) as LatestGpsPoint
    FROM positions
        WHERE deviceid IN
            (SELECT deviceid FROM device_group WHERE groupid = 7)
            AND valid = 1
            AND attributes LIKE '%FRI%'
            AND network != 'null'
            AND devicetime >= (SELECT deploymentdate 
                              FROM devices
                              WHERE id = deviceid
                              LIMIT 1
            )
            GROUP BY deviceid
    ) t2
ON t1.id = t2.LatestGpsPoint
ORDER BY t1.deviceid ASC
LIMIT 0,100

其他设备类型(dmt)查询如下所示:

SELECT t1.deviceid, t1.devicetime, t1.latitude, t1.longitude
FROM positions t1
INNER JOIN
    (SELECT MAX(id) as LatestGpsPoint
    FROM positions
        WHERE deviceid IN
            (SELECT deviceid FROM device_group WHERE groupid = 7)
            AND valid = 1
            AND devicetime >= (SELECT deploymentdate 
                              FROM devices
                              WHERE id = deviceid
                              LIMIT 1
            )
            GROUP BY deviceid
    ) t2
ON t1.id = t2.LatestGpsPoint
ORDER BY t1.deviceid ASC
LIMIT 0,100

我正在尝试在其中放置IF ELSE END语句,以根据t1.protocol ='gl200'或'dmt'更改搜索条件。

2 个答案:

答案 0 :(得分:0)

尝试UNION:

(SELECT t1.deviceid, t1.devicetime, t1.latitude, t1.longitude
FROM positions t1
INNER JOIN
    (SELECT MAX(id) as LatestGpsPoint
    FROM positions
        WHERE deviceid IN
            (SELECT deviceid FROM device_group WHERE groupid = 7)
            AND valid = 1
            AND attributes LIKE '%FRI%'
            AND network != 'null'
            AND devicetime >= (SELECT deploymentdate 
                              FROM devices
                              WHERE id = deviceid
                              LIMIT 1
            )
            GROUP BY deviceid
    ) t2
ON t1.id = t2.LatestGpsPoint)
UNION
(SELECT t1.deviceid, t1.devicetime, t1.latitude, t1.longitude
FROM positions t1
INNER JOIN
    (SELECT MAX(id) as LatestGpsPoint
    FROM positions
        WHERE deviceid IN
            (SELECT deviceid FROM device_group WHERE groupid = 7)
            AND valid = 1
            AND devicetime >= (SELECT deploymentdate 
                              FROM devices
                              WHERE id = deviceid
                              LIMIT 1
            )
            GROUP BY deviceid
    ) t2
ON t1.id = t2.LatestGpsPoint)

答案 1 :(得分:0)

if语句是可能的,但我认为你可以用case语句来做。代码的唯一区别是这部分:

SELECT t1.deviceid, t1.devicetime, t1.latitude, t1.longitude
FROM positions t1
INNER JOIN
    (SELECT MAX(id) as LatestGpsPoint
    FROM positions
        WHERE deviceid IN
            (SELECT deviceid FROM device_group WHERE groupid = 7)
            AND valid = 1
            AND CASE WHEN deviceID = 'gl200' --however you id the device
                      THEN attributes LIKE '%FRI%' --gl200
                      ELSE 1=1  --dmt
                END

            AND CASE WHEN deviceID = 'gl200' --however you id the device
                      THEN network != 'null' --gl200
                      ELSE 1=1 --dmt
                END
            AND devicetime >= (SELECT deploymentdate 
                              FROM devices
                              WHERE id = deviceid
                              LIMIT 1
            )
            GROUP BY deviceid
    ) t2
ON t1.id = t2.LatestGpsPoint
ORDER BY t1.deviceid ASC
LIMIT 0,100

仅适用于第一个查询,即GL200设备。

案例陈述将在angular.module('ecommerce').factory('CartService', ['$webSql', '$http', '$q', 'urls', function ($webSql, $http, $q, urls) { var addToCart = function () {//body of function} var factory = { addToCart: addToCart, removeCart: removeCart }; return factory;] } ]); 之后开始。

angular.module('ecommerce').factory('CartService',
['$webSql', '$http', '$q', 'urls',
    function ($webSql, $http, $q, urls) {
        function addTocart () {//Body of function}
        var factory = {
            addToCart: addToCart,
            removeCart: removeCart
        };

        return factory;]
    }
]);