我正在尝试从如下表中的字符串中提取一个值,
查询表,
query_id value
1 type={"page":"page"}&parent_id=10&image=on&content=on
2 type={"page":"page"}&parent_id=self
3 type={"category":"contact"}
如您所见, parent_id 在查询中,有时不在。
我想提取 parent_id 所以我得到这个结果,
query_id page_id value
1 10 type={"page":"page"}&parent_id=10&image=on&content=on
2 self type={"page":"page"}&parent_id=self
3 null type={"category":"contact"}
我尝试使用此查询,
SELECT
*,
CAST(
SUBSTRING(
value,PATINDEX('%parent_id=%', value) + 8,(PATINDEX('%&%', substring(value,PATINDEX('%parent_id=%', value),50)) - 9)
) AS INT
) AS page_id
FROM query
但是我收到此错误,
1064 - You have an error in your SQL syntax; check the manual that corresponds
to your MySQL server version for the right syntax to use
near 'INT ) AS page_id FROM query LIMIT 0, 30' at line 6
编辑:
也许我不应该使用PATINDEX
下面的查询测试它,
SELECT
*,
SUBSTRING(
value,PATINDEX('%parent_id=%', value) + 8,(PATINDEX('%&%', substring(value,PATINDEX('%parent_id=%', value),50)) - 9)
) AS test
FROM query
我得到这个错误,
#1305 - FUNCTION mydb.PATINDEX does not exist
编辑:
得到了我的答案,
SELECT
*,
IF(LOCATE('parent_id=', value)>0,SUBSTRING_INDEX(SUBSTRING(value,LOCATE('parent_id=', value) + 10),'&',1),null)AS page_id
FROM query