Guest User

Untitled

a guest
Jul 20th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. SELECT NULL AS msg_id, NULL AS total, NULL AS found
  2. FROM dual
  3. WHERE (
  4. @total :=0
  5. OR
  6. @found :=0
  7. )
  8. UNION
  9. SELECT msg_id, @total AS total, @found :=1 AS found
  10. FROM messages_queue
  11. WHERE (
  12. @total := @total + rcpts_count
  13. )
  14. AND @total <1000
  15. UNION
  16. SELECT msg_id, rcpts_count AS total, 0 AS found
  17. FROM messages_queue
  18. WHERE IF( @found =0, @found :=1, 0 )
  19.  
  20. -- sample table
  21. create table x
  22. (col1 int identity(1, 1)
  23. ,col2 varchar(50))
  24.  
  25. -- sample data
  26. insert into x (col2) values
  27. (null)
  28. ,(null)
  29. ,(null)
  30. ,(null)
  31. ,(null)
  32.  
  33. -- update from select
  34. update x
  35. set x.col2 = 'even'
  36. from x as [t2]
  37. where
  38. col1 = t2.col1
  39. and t2.col1 % 2 = 0
  40.  
  41. -- show results
  42. select * from x
  43.  
  44. -- clean up
  45. drop table x
  46.  
  47. UPDATE message_queue mq
  48. INNER JOIN (
  49. SELECT NULL AS msg_id, NULL AS total, NULL AS found
  50. FROM dual
  51. WHERE (
  52. @total :=0
  53. OR
  54. @found :=0
  55. )
  56. UNION
  57. SELECT msg_id, @total AS total, @found :=1 AS found
  58. FROM messages_queue
  59. WHERE (
  60. @total := @total + rcpts_count
  61. )
  62. AND @total <1000
  63. UNION
  64. SELECT msg_id, rcpts_count AS total, 0 AS found
  65. FROM messages_queue
  66. WHERE IF( @found =0, @found :=1, 0 )
  67. ) msgs ON msgs.msg_id = mq.msg_id
  68. SET mq.fet_id = 12345;
Add Comment
Please, Sign In to add comment