开发者

Tag repeating sequences in MySQL using two columns

I have a MySQL 5.5 database with a table that looks similar to this:

   id test location measurement   unit device retest
    1  1       1       -2405.5     mV    1     null
    2  1       2        856        mV    1     null
    3  1       3        956.4      mV    1     null
    4  2       1       -805.5      mV    1     null
    5  2       2        1456       mV    1     null
    6  2       3        22.4       mV    1     null
    7  1       1       -805.5      mV    1     null
    8  1       2        456        mV    1     null
    9  1       3        456.4      mV    1     null
   10  2       1       -805.5      mV    1     null
   11  2       2        456        mV    1     null
   12  2       3        456.4      mV    1     null
   13  1       1       -805.5      mV    2     null
   14  1       2        856        mV    2     null
   15  1       3        756.4      mV    2     null
   16  2       1       -805.5      mV    2     null
   17  2开发者_运维知识库       2        456        mV    2     null
   18  2       3        456.4      mV    2     null
   19  1       1       -805.5      mV    3     null
   20  1       2        456        mV    3     null
   21  1       3        456.4      mV    3     null
   22  2       1       -805.5      mV    3     null
   23  2       2        456        mV    3     null
   24  2       3        456.4      mV    3     null

I would like my table to reflect the number of times a device was retested:

   id test location measurement   unit device retest
    1  1       1       -2405.5     mV    1     0
    2  1       2        856        mV    1     0
    3  1       3        956.4      mV    1     0
    4  2       1       -805.5      mV    1     0
    5  2       2        1456       mV    1     0
    6  2       3        22.4       mV    1     0
    7  1       1       -805.5      mV    1     1
    8  1       2        456        mV    1     1
    9  1       3        456.4      mV    1     1
   10  2       1       -805.5      mV    1     1
   11  2       2        456        mV    1     1
   12  2       3        456.4      mV    1     l
   13  1       1       -805.5      mV    2     0
   14  1       2        856        mV    2     0
   15  1       3        756.4      mV    2     0
   16  2       1       -805.5      mV    2     0
   17  2       2        456        mV    2     0
   18  2       3        456.4      mV    2     0
   19  1       1       -805.5      mV    3     0
   20  1       2        456        mV    3     0
   21  1       3        456.4      mV    3     0
   22  2       1       -805.5      mV    3     0
   23  2       2        456        mV    3     0
   24  2       3        456.4      mV    3     0

I searched for days and all I found were techniques for counting the number of duplicates. I'm certain that the key lies in a combination of the test, location and device columns, but I lack the skills. Thank you for any assistance you would care to provide.


SELECT  *,
        EXISTS
        (
        SELECT  NULL
        FROM    mytable mr
        WHERE   mr.test = m.test
                AND mr.location = m.location
                AND mr.device = m.device
                AND mr.id < m.id
        ) AS retest
FROM    mytable m

Create an index on (test, location, device, id) for this to work fast.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜