2

我正在尝试从如下表中的字符串中提取一个值,

查询表,

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
4

1 回答 1

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`
于 2012-05-01T12:21:33.363 回答