簡體   English   中英

使用mysql查詢找到具有特定值的4個連續行

[英]find 4 consecutive rows with a specific value using a mysql query

我的表有4列:id(自動遞增),machine(1到300之間的整數),event(整數),status(字符串)

我需要在該表上運行一個查詢,該查詢返回的四台機器的機器數最低,其中event =“ 5”和status =“ free”且是連續數字。

例如,如果計算機3繁忙,則查詢不應返回1,2,4,5,因為它們不是結果。 相反,如果機器4,5,6,7是空閑的,則應返回這些機器。 它不應返回5、6、7、8,因為它不是最低的機器編號。 最低可能是4,5,6,7。

select * from mytable where event="5" and status="free" order by machine asc limit 4

確實執行了我需要的操作,除了它確實返回了所有行,沒有考慮到它們必須在機器列中出現。

能做到嗎?

sample data as requested:

id - machine - event - status
22      1        5      free
23      2        5      free
24      3        5      busy
25      4        5      busy
26      5        5      free  *
27      6        5      free  *
28      7        5      free  *
29      8        5      free  *
30      9        5      free
31      10       5      busy
32      11       5      free

標有*的行是我需要獲得的行。 列計算機上的前4個連續行,狀態值為free,事件id = 5。

如果我理解正確,這應該可以解決問題

select  t1.id, t2.id, t3.id, t4.id
from    (select * from mytable where event="5" and status="free") t1
join    (select * from mytable where event="5" and status="free") t2
on      t1.id + 1 = t2.id
join    (select * from mytable where event="5" and status="free") t3
on      t1.id + 2 = t3.id
join    (select * from mytable where event="5" and status="free") t4
on      t1.id + 3 = t4.id
order by t1.id
limit 1

可能需要對連接條件進行一些調整(沒有示例很難說明)。

這樣的事情應該工作

    SELECT SUBSTRING_INDEX(GROUP_CONCAT(machine ORDER BY machine),',',4) as machines FROM
(SELECT machine,
CASE WHEN machine=@machine + 1 THEN @n ELSE @n:=@n+1 END AS g,
    @machine := machine As m
  FROM
    t, (SELECT @n:=0) r, (SELECT @machine := '') z
WHERE event=5 and status="free"
  ORDER BY
    id) sub
    GROUP BY g 
    HAVING COUNT(*) >=4

如果要所有行

SELECT t.id,t.machine,t.event,t.status FROM
(SELECT id,machine,event,status,GROUP_CONCAT(id ORDER BY id) gr FROM
(SELECT *,
CASE WHEN machine=@machine + 1 THEN @n ELSE @n:=@n+1 END AS g,
    @machine := machine As m
  FROM
    t, (SELECT @n:=0) r, (SELECT @machine := '') z
WHERE event=5 and status="free"
  ORDER BY
    id) sub
    GROUP BY g 
    HAVING COUNT(*) >=4) o
JOIN t ON FIND_IN_SET(t.id,gr)
ORDER BY id LIMIT 4

測試

測試2

這在某種程度上取決於您的數據集的大小,但是對於小型數據集,這應該起作用:

select * from mytable m1 where m1.event="5" and m1.status="free" 
and(m1.machine-1 in (select machine from mytable where event="5" and status="free") 
and m1.machine-2 in (select machine from mytable where event="5" and status="free") 
and m1.machine-3 in (select machine from mytable where event="5" and status="free") 
OR( m1.machine+1 in (select machine from mytable where event="5" and status="free") 
and m1.machine+2 in (select machine from mytable where event="5" and status="free") 
and m1.machine+3 in (select machine from mytable where event="5" and status="free") )
order by m1.machine asc limit 4

對於較大的數據集,您將需要使用JOIN來完成相同的操作。

您也可以只使用后半部分找到第一個實例,然后從那里開始查詢。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM