我正在尝试从下面这样的表中的字符串中提取值,
查询表,
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
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`