二手委托人表创建.sql 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. -- ============================================
  2. -- 二手委托人信息表 INSERT SQL 模板
  3. -- ============================================
  4. -- 方式1: 插入完整数据(推荐)
  5. INSERT INTO second_entrust_user (
  6. entrust_trade_id,
  7. entrust_trade_no,
  8. entrust_user_phone,
  9. entrust_user_name,
  10. entrust_id_card,
  11. entrust_id_card_img,
  12. delete_flag,
  13. created_time,
  14. created_user_id,
  15. updated_time,
  16. updated_user_id
  17. ) VALUES (
  18. 1, -- entrust_trade_id: 交易ID
  19. 'TRADE202511210001', -- entrust_trade_no: 交易编号
  20. '13800138000', -- entrust_user_phone: 委托人电话
  21. '张三', -- entrust_user_name: 委托人姓名
  22. '110101199001011234', -- entrust_id_card: 委托人身份证
  23. 'https://oss.example.com/id/123.jpg', -- entrust_id_card_img: 委托人身份证照片
  24. 0, -- delete_flag: 删除标记(0:未删除, 1:已删除)
  25. NOW(), -- created_time: 创建时间
  26. 1, -- created_user_id: 创建人ID
  27. NOW(), -- updated_time: 修改时间
  28. 1 -- updated_user_id: 修改人ID
  29. );
  30. -- 方式2: 插入必填字段(让数据库自动填充其他字段)
  31. INSERT INTO second_entrust_user (
  32. entrust_trade_id,
  33. entrust_trade_no,
  34. entrust_user_phone,
  35. entrust_user_name,
  36. created_time
  37. ) VALUES (
  38. 2, -- entrust_trade_id
  39. 'TRADE202511210002', -- entrust_trade_no
  40. '13900139000', -- entrust_user_phone
  41. '李四', -- entrust_user_name
  42. NOW() -- created_time
  43. );
  44. -- 方式3: 批量插入
  45. INSERT INTO second_entrust_user (
  46. entrust_trade_id,
  47. entrust_trade_no,
  48. entrust_user_phone,
  49. entrust_user_name,
  50. entrust_id_card,
  51. entrust_id_card_img,
  52. delete_flag,
  53. created_time
  54. ) VALUES
  55. (3, 'TRADE202511210003', '13700137000', '王五', '110101199001011235', 'https://oss.example.com/id/234.jpg', 0, NOW()),
  56. (4, 'TRADE202511210004', '13600136000', '赵六', '110101199001011236', 'https://oss.example.com/id/345.jpg', 0, NOW()),
  57. (5, 'TRADE202511210005', '13500135000', '孙七', '110101199001011237', 'https://oss.example.com/id/456.jpg', 0, NOW());
  58. -- ============================================
  59. -- 查询语句
  60. -- ============================================
  61. -- 查询所有未删除的委托人信息
  62. SELECT * FROM second_entrust_user WHERE delete_flag = 0;
  63. -- 根据交易ID查询
  64. SELECT * FROM second_entrust_user WHERE entrust_trade_id = 1 AND delete_flag = 0;
  65. -- 根据交易编号查询
  66. SELECT * FROM second_entrust_user WHERE entrust_trade_no = 'TRADE202511210001' AND delete_flag = 0;
  67. -- 根据委托人电话查询
  68. SELECT * FROM second_entrust_user WHERE entrust_user_phone = '13800138000' AND delete_flag = 0;
  69. -- 根据委托人姓名模糊查询
  70. SELECT * FROM second_entrust_user WHERE entrust_user_name LIKE '%张%' AND delete_flag = 0;
  71. -- 关联交易记录表查询
  72. SELECT
  73. seu.*,
  74. str.trade_no,
  75. str.transaction_time,
  76. str.transaction_location,
  77. str.trade_status
  78. FROM second_entrust_user seu
  79. LEFT JOIN second_trade_record str ON seu.entrust_trade_id = str.id
  80. WHERE seu.delete_flag = 0;
  81. -- ============================================
  82. -- 更新语句
  83. -- ============================================
  84. -- 更新委托人信息
  85. UPDATE second_entrust_user
  86. SET
  87. entrust_user_phone = '13800138001',
  88. entrust_user_name = '张三丰',
  89. entrust_id_card = '110101199001011238',
  90. updated_time = NOW(),
  91. updated_user_id = 1
  92. WHERE id = 1 AND delete_flag = 0;
  93. -- ============================================
  94. -- 删除语句(逻辑删除)
  95. -- ============================================
  96. -- 逻辑删除
  97. UPDATE second_entrust_user
  98. SET
  99. delete_flag = 1,
  100. updated_time = NOW(),
  101. updated_user_id = 1
  102. WHERE id = 1;
  103. -- 物理删除(谨慎使用)
  104. -- DELETE FROM second_entrust_user WHERE id = 1;
  105. -- ============================================
  106. -- 统计查询
  107. -- ============================================
  108. -- 统计委托人总数
  109. SELECT COUNT(*) as total FROM second_entrust_user WHERE delete_flag = 0;
  110. -- 按日期统计委托人创建数量
  111. SELECT
  112. DATE(created_time) as create_date,
  113. COUNT(*) as count
  114. FROM second_entrust_user
  115. WHERE delete_flag = 0
  116. GROUP BY DATE(created_time)
  117. ORDER BY create_date DESC;
  118. -- 查询最近创建的10条委托人信息
  119. SELECT * FROM second_entrust_user
  120. WHERE delete_flag = 0
  121. ORDER BY created_time DESC
  122. LIMIT 10;