我的机器人每日前30名排行通过以下MSSQL来查询 STEEMSQL Comments 表中 标签中有 cn 或者 china 的帖子或者评论:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | select top 10 title from Comments (NOLOCK) where (category='cn') or ( ( ( JSON_VALUE(json_metadata,'$.tags[4]') in ('cn','china') ) or ( JSON_VALUE(json_metadata,'$.tags[3]') in ('cn','china') ) or ( JSON_VALUE(json_metadata,'$.tags[2]') in ('cn','china') ) or ( JSON_VALUE(json_metadata,'$.tags[1]') in ('cn','china') ) or ( JSON_VALUE(json_metadata,'$.tags[0]') in ('cn','china') ) ) ) order by created desc |
select top 10 title from Comments (NOLOCK) where (category='cn') or ( ( ( JSON_VALUE(json_metadata,'$.tags[4]') in ('cn','china') ) or ( JSON_VALUE(json_metadata,'$.tags[3]') in ('cn','china') ) or ( JSON_VALUE(json_metadata,'$.tags[2]') in ('cn','china') ) or ( JSON_VALUE(json_metadata,'$.tags[1]') in ('cn','china') ) or ( JSON_VALUE(json_metadata,'$.tags[0]') in ('cn','china') ) ) ) order by created desc
主标签在 category 字段里定义, 但是其它标签则被保存于JSON格式的字符串, 所以我们需要通过 JSON_VALUE 这个函数来解析JSON, 有时候当被解析的字符串是空或者无效时, SQL就出错了:
Error 13609: JSON text is not properly formatted. Unexpected character ‘.’ is found at position 0.
最简单的解决方法就是通过布尔短路, 在 JSON_VALUE 前加入有效性判断:
1 | ISJSON(json_metadataa) > 0 |
ISJSON(json_metadataa) > 0
正确的版本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | select top 10 title from Comments (NOLOCK) where (category='cn') or ( (ISJSON(json_metadataa) > 0) and ( ( JSON_VALUE(json_metadata,'$.tags[4]') in ('cn','china') ) or ( JSON_VALUE(json_metadata,'$.tags[3]') in ('cn','china') ) or ( JSON_VALUE(json_metadata,'$.tags[2]') in ('cn','china') ) or ( JSON_VALUE(json_metadata,'$.tags[1]') in ('cn','china') ) or ( JSON_VALUE(json_metadata,'$.tags[0]') in ('cn','china') ) ) ) order by created desc |
select top 10 title from Comments (NOLOCK) where (category='cn') or ( (ISJSON(json_metadataa) > 0) and ( ( JSON_VALUE(json_metadata,'$.tags[4]') in ('cn','china') ) or ( JSON_VALUE(json_metadata,'$.tags[3]') in ('cn','china') ) or ( JSON_VALUE(json_metadata,'$.tags[2]') in ('cn','china') ) or ( JSON_VALUE(json_metadata,'$.tags[1]') in ('cn','china') ) or ( JSON_VALUE(json_metadata,'$.tags[0]') in ('cn','china') ) ) ) order by created desc
如果我们把 ISJSON 放在 JSON_VALUE 之后, 起不到布尔短路的作用, 因为查询还是会对每组数据(包括无效)进行 JSON_VALUE解析.
英文: SteemSQL Tutorial – How to Fix “JSON text is not properly formated. Unexpected character ‘.’ is found at position 0.”?
STEEM SQL 系列教程
- STEEM SQL 系列教程之 随机返回是怎么实现的?
- STEEM SQL 系列教程之 如何获取最近7天 CN 区用户发贴量, 点赞数和估计收益值?
- STEEM SQL 系列教程之 历史上的今天怎么实现的?
- STEEM SQL 系列教程之 每个月能挣多少?
- STEEM SQL 系列教程之 真的可以恢复删除的文章或评论么?
- STEEM SQL 系列教程之 大鱼们都给谁投票了?
- STEEM SQL 系列教程之 办银行一个月共发了多少利息?
- STEEM SQL 系列教程之 你的哪些好友已经好久没玩STEEM了?
- STEEM SQL 系列教程之 如何使用 ISJSON 和 JSON_VALUE 函数?
- STEEM SQL 系列教程之 如何获取史上赚最多金的作者?
- STEEM SQL 系列教程之 一鸣惊人的作者
- STEEM SQL 系列教程之 如何避免 SQL 注入?
- STEEM SQL 系列教程之 我花了800多 SBD (7000多美元)买赞
- STEEM SQL 系列教程之 怎么样查看 STEEMSQL 是否数据同步完全?
GD Star Rating
loading...
本文一共 396 个汉字, 你数一下对不对.loading...
上一篇: 孩子掉牙, 牙齿精灵来了
下一篇: 英国小病去不了急诊会急死人
扫描二维码,分享本文到微信朋友圈