使用mysql中的状态列更新基于最小数的列?

时间:2017-02-18 17:07:09

标签: mysql sql

我想更新状态= 8的数字2

UPDATE tqueue 
SET status = '8' 
WHERE (SELECT MIN(number) 
       FROM tqueue
       WHERE STATUS IN ('4')
         AND get_ticket >= CURDATE( ) 
         AND get_ticket < DATE_ADD( CURDATE(), INTERVAL 1 DAY) 
       GROUP BY service)

输出:

--------------------------------------------------
number   |    status   |  get_ticket 
--------------------------------------------------
   3            4         2017-02-18  13:43:01
   2            4         2017-02-18  12:34:03
   1            8         2017-02-18  10:04:59   

1 个答案:

答案 0 :(得分:0)

您是否正在寻找将number中的最低status=8更新为status=4

试试这个:

 SELECT @mynumber:=number FROM tqueue
 WHERE STATUS = '4'
 AND get_ticket >= CURDATE( )
 AND get_ticket < DATE_ADD( CURDATE() , INTERVAL 1 DAY )
 GROUP BY service
 order by number asc
 limit 0,1 ;

 UPDATE tqueue SET status = '8' where number = @mynumber ;

此外,您可以使用临时表进行更新自我引用:

 CREATE TEMPORARY TABLE mytbl_numbers AS 
 SELECT number FROM tqueue
 WHERE STATUS = '4'
 AND get_ticket >= CURDATE( )
 AND get_ticket < DATE_ADD( CURDATE() , INTERVAL 1 DAY )
 GROUP BY service
 order by number asc
 limit 0,1 ;

 UPDATE tqueue SET status = '8' where number in (SELECT * FROM  mytbl_numbers) ;

更有用的答案可以在[+]&amp; [+]&amp; ...