当前位置:   article > 正文

记录一次如何查询mysql分库分表数据_分库分表查询

分库分表查询

 一、前言

本次查询是在未知如何分库分表的情况下,对表数据进行查询,其中有的字段为JSON结构。需要提取JSON中某个字段的内容。

二、查询步骤

1、第一方式是将所有分表数据进行union  all

  1. select * from apporder.ord_shopping_order
  2. union all
  3. select * from apporder.ord_shopping_order_2
  4. union all
  5. select * from apporder.ord_shopping_order_3
  6. union all
  7. select * from apporder.ord_shopping_order_4
  8. union all
  9. select * from apporder.ord_shopping_order_5
  10. union all
  11. select * from apporder.ord_shopping_order_6
  12. union all
  13. select * from apporder.ord_shopping_order_7
  14. union all
  15. select * from apporder.ord_shopping_order_8
  16. union all
  17. select * from apporder.ord_shopping_order_9
  18. union all
  19. select * from apporder.ord_shopping_order_10
  20. union all
  21. select * from apporder.ord_shopping_order_11
  22. union all
  23. select * from apporder.ord_shopping_order_12
  24. union all
  25. select * from apporder.ord_shopping_order_13
  26. union all
  27. select * from apporder.ord_shopping_order_14
  28. union all
  29. select * from apporder.ord_shopping_order_15
  30. union all
  31. select * from apporder.ord_shopping_order_16
  32. union all
  33. select * from apporder.ord_shopping_order_17
  34. union all
  35. select * from apporder.ord_shopping_order_18
  36. union all
  37. select * from apporder.ord_shopping_order_19
  38. union all
  39. select * from apporder.ord_shopping_order_20
  40. union all
  41. select * from apporder.ord_shopping_order_21
  42. union all
  43. select * from apporder.ord_shopping_order_22
  44. union all
  45. select * from apporder.ord_shopping_order_23
  46. union all
  47. select * from apporder.ord_shopping_order_24
  48. union all
  49. select * from apporder.ord_shopping_order_25
  50. union all
  51. select * from apporder.ord_shopping_order_26
  52. union all
  53. select * from apporder.ord_shopping_order_27
  54. union all
  55. select * from apporder.ord_shopping_order_28
  56. union all
  57. select * from apporder.ord_shopping_order_29
  58. union all
  59. select * from apporder.ord_shopping_order_30
  60. union all
  61. select * from apporder.ord_shopping_order_31
  62. union all
  63. select * from apporder.ord_shopping_order_32
  64. union all
  65. select * from apporder.ord_shopping_order_33
  66. union all
  67. select * from apporder.ord_shopping_order_34
  68. union all
  69. select * from apporder.ord_shopping_order_35
  70. union all
  71. select * from apporder.ord_shopping_order_36
  72. union all
  73. select * from apporder.ord_shopping_order_37
  74. union all
  75. select * from apporder.ord_shopping_order_38
  76. union all
  77. select * from apporder.ord_shopping_order_39
  78. union all
  79. select * from apporder.ord_shopping_order_40
  80. union all
  81. select * from apporder.ord_shopping_order_41
  82. union all
  83. select * from apporder.ord_shopping_order_42
  84. union all
  85. select * from apporder.ord_shopping_order_43
  86. union all
  87. select * from apporder.ord_shopping_order_44
  88. union all
  89. select * from apporder.ord_shopping_order_45
  90. union all
  91. select * from apporder.ord_shopping_order_46
  92. union all
  93. select * from apporder.ord_shopping_order_47
  94. union all
  95. select * from apporder.ord_shopping_order_48
  96. union all
  97. select * from apporder.ord_shopping_order_49
  98. union all
  99. select * from apporder.ord_shopping_order_50
  100. union all
  101. select * from apporder.ord_shopping_order_51
  102. union all
  103. select * from apporder.ord_shopping_order_52
  104. union all
  105. select * from apporder.ord_shopping_order_53
  106. union all
  107. select * from apporder.ord_shopping_order_54
  108. union all
  109. select * from apporder.ord_shopping_order_55
  110. union all
  111. select * from apporder.ord_shopping_order_56
  112. union all
  113. select * from apporder.ord_shopping_order_57
  114. union all
  115. select * from apporder.ord_shopping_order_58
  116. union all
  117. select * from apporder.ord_shopping_order_59
  118. union all
  119. select * from apporder.ord_shopping_order_60
  120. union all
  121. select * from apporder.ord_shopping_order_61
  122. union all
  123. select * from apporder.ord_shopping_order_62
  124. union all
  125. select * from apporder.ord_shopping_order_63;

2、第二种方式是创建一个存储过程,方便以后更快速查询

先建一个表结构与  分表一样的表 ord_shopping_procedure

create table ord_shopping_procedure as  select  * from  ord_shopping_order_2;

create table ord_shopping_procedure as  select  * from  ord_shopping_order_2

然后创建存储过程

  1. CREATE DEFINER=`root`@`%` PROCEDURE `apporder`.`ord_shopping`()
  2. begin
  3. truncate table ord_shopping_procedure;
  4. insert into ord_shopping_procedure
  5. select * from apporder.ord_shopping_order
  6. union all
  7. select * from apporder.ord_shopping_order_2
  8. union all
  9. select * from apporder.ord_shopping_order_3
  10. union all
  11. select * from apporder.ord_shopping_order_4
  12. union all
  13. select * from apporder.ord_shopping_order_5
  14. union all
  15. select * from apporder.ord_shopping_order_6
  16. union all
  17. select * from apporder.ord_shopping_order_7
  18. union all
  19. select * from apporder.ord_shopping_order_8
  20. union all
  21. select * from apporder.ord_shopping_order_9
  22. union all
  23. select * from apporder.ord_shopping_order_10
  24. union all
  25. select * from apporder.ord_shopping_order_11
  26. union all
  27. select * from apporder.ord_shopping_order_12
  28. union all
  29. select * from apporder.ord_shopping_order_13
  30. union all
  31. select * from apporder.ord_shopping_order_14
  32. union all
  33. select * from apporder.ord_shopping_order_15
  34. union all
  35. select * from apporder.ord_shopping_order_16
  36. union all
  37. select * from apporder.ord_shopping_order_17
  38. union all
  39. select * from apporder.ord_shopping_order_18
  40. union all
  41. select * from apporder.ord_shopping_order_19
  42. union all
  43. select * from apporder.ord_shopping_order_20
  44. union all
  45. select * from apporder.ord_shopping_order_21
  46. union all
  47. select * from apporder.ord_shopping_order_22
  48. union all
  49. select * from apporder.ord_shopping_order_23
  50. union all
  51. select * from apporder.ord_shopping_order_24
  52. union all
  53. select * from apporder.ord_shopping_order_25
  54. union all
  55. select * from apporder.ord_shopping_order_26
  56. union all
  57. select * from apporder.ord_shopping_order_27
  58. union all
  59. select * from apporder.ord_shopping_order_28
  60. union all
  61. select * from apporder.ord_shopping_order_29
  62. union all
  63. select * from apporder.ord_shopping_order_30
  64. union all
  65. select * from apporder.ord_shopping_order_31
  66. union all
  67. select * from apporder.ord_shopping_order_32
  68. union all
  69. select * from apporder.ord_shopping_order_33
  70. union all
  71. select * from apporder.ord_shopping_order_34
  72. union all
  73. select * from apporder.ord_shopping_order_35
  74. union all
  75. select * from apporder.ord_shopping_order_36
  76. union all
  77. select * from apporder.ord_shopping_order_37
  78. union all
  79. select * from apporder.ord_shopping_order_38
  80. union all
  81. select * from apporder.ord_shopping_order_39
  82. union all
  83. select * from apporder.ord_shopping_order_40
  84. union all
  85. select * from apporder.ord_shopping_order_41
  86. union all
  87. select * from apporder.ord_shopping_order_42
  88. union all
  89. select * from apporder.ord_shopping_order_43
  90. union all
  91. select * from apporder.ord_shopping_order_44
  92. union all
  93. select * from apporder.ord_shopping_order_45
  94. union all
  95. select * from apporder.ord_shopping_order_46
  96. union all
  97. select * from apporder.ord_shopping_order_47
  98. union all
  99. select * from apporder.ord_shopping_order_48
  100. union all
  101. select * from apporder.ord_shopping_order_49
  102. union all
  103. select * from apporder.ord_shopping_order_50
  104. union all
  105. select * from apporder.ord_shopping_order_51
  106. union all
  107. select * from apporder.ord_shopping_order_52
  108. union all
  109. select * from apporder.ord_shopping_order_53
  110. union all
  111. select * from apporder.ord_shopping_order_54
  112. union all
  113. select * from apporder.ord_shopping_order_55
  114. union all
  115. select * from apporder.ord_shopping_order_56
  116. union all
  117. select * from apporder.ord_shopping_order_57
  118. union all
  119. select * from apporder.ord_shopping_order_58
  120. union all
  121. select * from apporder.ord_shopping_order_59
  122. union all
  123. select * from apporder.ord_shopping_order_60
  124. union all
  125. select * from apporder.ord_shopping_order_61
  126. union all
  127. select * from apporder.ord_shopping_order_62
  128. union all
  129. select * from apporder.ord_shopping_order_63;
  130. truncate table ord_shopping;
  131. insert into ord_shopping
  132. select t.*, JSON_EXTRACT(t.json, '$.skuName') as sku_name from (
  133. select id,order_type,order_mode,user_id,user_name,trade_no,state,total_amount, pay_amount pay_time ,
  134. ou_name ,mobile ,create_time, sale_amount, JSON_EXTRACT(item_abstract_json, '$[0]') as json,store_ou_code,store_name
  135. ,mch_ou_code,mch_name
  136. from ord_shopping_procedure ) t;
  137. END;

由于表中存在JSON字段,同时我们再查询表是应查询需要的字段即可

所以这一段是重点

truncate table ord_shopping;
insert into   ord_shopping
select t.*, JSON_EXTRACT(t.json, '$.skuName') as sku_name from  (
select id,order_type,order_mode,user_id,user_name,trade_no,state,total_amount, pay_amount pay_time ,
ou_name ,mobile  ,create_time, sale_amount, JSON_EXTRACT(item_abstract_json, '$[0]') as json,store_ou_code,store_name
,mch_ou_code,mch_name
from  ord_shopping_procedure ) t;

  1. truncate table ord_shopping;
  2. insert into ord_shopping
  3. select t.*, JSON_EXTRACT(t.json, '$.skuName') as sku_name from (
  4. select id,order_type,order_mode,user_id,user_name,trade_no,state,total_amount, pay_amount pay_time ,
  5. ou_name ,mobile ,create_time, sale_amount, JSON_EXTRACT(item_abstract_json, '$[0]') as json,store_ou_code,store_name
  6. ,mch_ou_code,mch_name
  7. from ord_shopping_procedure ) t;

JSON_EXTRACT(item_abstract_json, '$[0]') as json

表示item_abstract_json这字段存储的JSON字符串数组,我们把下标为0的取出来,

样例数据是这样的

  1. [{
  2. "id": 1059,
  3. "productType": 1,
  4. "productTypeSub": null,
  5. "skuName": "L’Oréal 欧莱雅男士锐能抗皱紧致洁面膏",
  6. "skuId": 194538,
  7. "skuCode": "200015112056194538",
  8. "thirdCode": "C023338",
  9. "isGift": 0,
  10. "scale": null,
  11. "unit": null,
  12. "brandId": 1707,
  13. "storeOuCode": "1043002001",
  14. "number": 1,
  15. "specs": "规格:100ML ",
  16. "cls1StaId": 1316,
  17. "cls2StaId": 16831,
  18. "cls3StaId": 16840,
  19. "cls1StaName": "美妆护肤",
  20. "cls2StaName": "男士护肤",
  21. "cls3StaName": "男士洁面",
  22. "phPictureUrl": "https:///default_file/00/01/52/02.jpg",
  23. "activityId": null,
  24. "activityTitle": null,
  25. "selfActivityId": null,
  26. "selfActivityTitle": null,
  27. "selfActivityType": null,
  28. "randomDeductionActivityId": null,
  29. "randomDeductionActivityTitle": null,
  30. "salePrice": 28.0,
  31. "realPrice": 28,
  32. "pointsPrice": 0,
  33. "rewardsPrice": 0,
  34. "empRewardsPrice": 0,
  35. "enterprisePrice": 0,
  36. "eleType": null,
  37. "eleDetailJson": null,
  38. "thirdServeId": null,
  39. "lineNo": "1",
  40. "packageItem": [],
  41. "barcode": null,
  42. "exchangeJumpAddress": null,
  43. "skuSaleScale": null,
  44. "perWeight": null,
  45. "externalNumber": null,
  46. "externalPrice": null,
  47. "deliverType": 2,
  48. "serveHour": null,
  49. "serveFlag": 0,
  50. "qyMerchant": null,
  51. "qyAlias": null,
  52. "distributionHigherReferrerId": null,
  53. "thirdpartyDeliveryTime": null,
  54. "thirdpartyCompleteTime": null
  55. }]

然后我们只需要取JSON的 skuName的内容就要用到这个函数

JSON_EXTRACT(t.json, '$.skuName') as sku_name

执行完存储过程,最后我们查询一下这个表ord_shopping

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/407066
推荐阅读
相关标签
  

闽ICP备14008679号