11upsert.mql 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. -- ============================================================
  2. -- Upsert 操作测试
  3. -- ============================================================
  4. -- 1. 基本 upsert - 插入新数据(name不存在)
  5. insert into /test/aaa/test_conflict (c_name, c_value, c_count) values ('conflict_upsert_001', 500, 1) on conflict update c_value=c_value
  6. ;
  7. select * from /test/aaa/test_conflict where c_name='conflict_upsert_001'
  8. /**
  9. output()
  10. count(1)
  11. **/
  12. ;
  13. -- 2. upsert - 更新已存在数据
  14. insert into /test/aaa/test_conflict (c_name, c_value, c_count) values ('conflict_upsert_001', 999, 5) on conflict update c_value=c_value, c_count=c_count
  15. ;
  16. select c_value, c_count from /test/aaa/test_conflict where c_name='conflict_upsert_001'
  17. /**
  18. output()
  19. match("c_value","999")
  20. match("c_count","5")
  21. **/
  22. ;
  23. -- 3. upsert - 累加 c_count
  24. insert into /test/aaa/test_conflict (c_name, c_value, c_count) values ('conflict_002', 200, 5) on conflict update c_count=c_count+c_count
  25. ;
  26. select c_count from /test/aaa/test_conflict where c_name='conflict_002'
  27. /**
  28. output()
  29. match("c_count","6")
  30. **/
  31. ;
  32. -- 4. upsert - 保留某些字段不更新
  33. insert into /test/aaa/test_conflict (c_name, c_value, c_count) values ('conflict_001', 888, 10) on conflict update c_count=c_count+c_count
  34. ;
  35. select c_value, c_count from /test/aaa/test_conflict where c_name='conflict_001'
  36. /**
  37. output()
  38. match("c_value","100")
  39. match("c_count","11")
  40. **/
  41. ;