mysql到redis_mysql数据导入redis

先编写sql脚本,查询好需要导入redis的数据,同时在脚本中设定好redis的类型;最外层的查询需要设定好查询的字段数量,key value各算一个。

我用的是hash结构,将表明以key的形式存储,字段+对应的值以json格式存储,脚本在图片下方

脚本上传到服务器后使用此命令导入数据(用户名密码、脚本路径自行更改):mysql -uroot -p123456 -h 127.0.0.1 database--skip-column-names --raw

27b4644b4f512d3c75b8af12401970ba.png

SELECT CONCAT(

"*4\r\n",

'$', LENGTH(redis_cmd), '\r\n',

redis_cmd, '\r\n',

'$', LENGTH(redis_key), '\r\n',

redis_key, '\r\n',

'$',LENGTH(hkey),'\r\n',hkey,'\r\n',

'$',LENGTH(hval),'\r\n',hval,'\r\n'

)

FROM(

SELECT

'HSET' AS redis_cmd,

'tab_product' AS redis_key,

a.product_id AS hkey,

CONCAT(

'{"id":"', a.id,'"',

',"productId":"', a.product_id,'"',

',"shopId":"', a.shop_id,'"',

',"title":"', a.title,'"',

',"originalPrice":"', a.original_price,'"',

',"price":"', a.price,'"',

',"info":"', a.info,'"',

',"createTime":"', a.create_time,'"',

',"updateTime":"', ifnull(a.update_time,''),'"',

',"status":"', a.status,'"',

',"img":"', a.img,'"',

',"upperLimit":"', a.upper_limit,'"',

',"endTime":"', ifnull(a.end_time,''),'"',

',"refushReason":"', ifnull(a.refush_reason,''),'"',

',"putawayDate":"', ifnull(a.putaway_date,''),'"',

',"tagId":"', ifnull(c.tag_id,''),'"',

',"tagName":"', ifnull(d.title,''),'"',

',"shopName":"', ifnull(e.shop_name,''),'"',

',"address":"', ifnull(e.address,''),'"',

',"businessHours":"', ifnull(e.business_hours,''),'"',

',"pension":"', round(ifnull(e.pension_scale,0) * a.price, 2),'"',

',"productImageList":', ifnull(CONCAT('[',GROUP_CONCAT('{"img":"',b.img, '"}'),']'),''),'}'

) AS hval

FROM `tab_product` a

LEFT JOIN tab_product_image b on a.product_id = b.product_id and b.state = 0

LEFT JOIN tab_product_tag c on c.product_id = a.product_id and c.state = 0

LEFT JOIN tab_tag d on d.id = c.tag_id and d.state = 0

LEFT JOIN shop.tab_shop e on e.shop_id = a.shop_id

group by a.id

) AS t

这是HSET结构,用到的改一下上面的就可以了

91a0d4622916781cce5271417c72a889.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值