我正在尝试从如下表格中的字符串中提取值,
查询表,
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
答案 0 :(得分:3)
INT已在MYSQL中使用。使用正确的语法
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`