Pandaaaa906

Untitled

Jan 15th, 2024 (edited)
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- 删除smiles为空的
  2. DELETE FROM "t_prod_smiles" struct WHERE smiles IS NULL;
  3.  
  4. -- 删除smiles是错的的
  5. DELETE FROM "t_prod_smiles" struct WHERE bingo.checkmolecule(smiles) IS NOT NULL;
  6.  
  7. -- 删除t_product没有相关记录的
  8. with to_delete as (
  9.     SELECT
  10.     struct.*
  11.     FROM "t_prod_smiles" struct
  12.  
  13.     LEFT JOIN t_product prd
  14.     ON prd.id = struct."id"
  15.  
  16.     WHERE prd.id is null
  17. )
  18. DELETE FROM "t_prod_smiles" struct
  19. USING to_delete
  20. WHERE struct.id = to_delete.id;
  21.  
  22. -- 更新结构式不一致的
  23. with tmp as (
  24.     SELECT
  25.     struct.*,
  26.     COALESCE(prd.smiles, '') new_smiles
  27.     FROM t_prod_smiles struct
  28.  
  29.     LEFT JOIN t_product prd
  30.     ON prd.id = struct."id"
  31.  
  32.     WHERE COALESCE(prd.smiles, '') != COALESCE(struct.smiles, '')
  33. ),to_update as (
  34.     SELECT
  35.     tmp.*
  36.     FROM tmp
  37.  
  38.     WHERE bingo.checkmolecule(tmp.new_smiles) is null
  39. )
  40. -- SELECT * FROM to_update
  41. UPDATE t_prod_smiles struct
  42. SET smiles = to_update.new_smiles,
  43. update_time = now()
  44. FROM to_update
  45. WHERE to_update.id = struct.id;
  46.  
  47. -- 新增
  48. with tmp as (
  49.     SELECT
  50.     prd.id,
  51.     COALESCE(prd.smiles, '') smiles,
  52.     now() create_time,
  53.     now() update_time,
  54.     1 is_delete
  55.     FROM t_product prd
  56.  
  57.     LEFT JOIN t_prod_smiles struct
  58.     ON prd.id = struct."id"
  59.  
  60.     WHERE 1=1
  61.     AND struct.id is null
  62.     AND COALESCE(prd.smiles, '') != ''
  63. )
  64. INSERT INTO t_prod_smiles(id, smiles, create_time, update_time, is_delete)
  65. SELECT
  66. tmp.*
  67. FROM tmp
  68.  
  69. WHERE 1=1
  70. AND bingo.checkmolecule(tmp.smiles) is null
  71.  
Add Comment
Please, Sign In to add comment