Mysql select 仅来自子字符串结果的数字

Mysql select only numbers from substring result

我有这个查询

SELECT SUBSTRING(post_content,INSTR(post_content, '[table'),16) as substring,
ID FROM `wp276_posts`
WHERE `post_content` LIKE '%[table%' 
AND `post_status` = 'publish' 
ORDER BY `ID` ASC 

returns 结果:

我怎样才能 select 只有子字符串的数字部分?

例如所需结果的数量:

+-----------+------+
| substring | ID   |
+-----------+------+
| 22        | 5072 |
+-----------+------+
| 67        | 5757 |
+-----------+------+
| 54        | 8550 |
+-----------+------+

SUBSTRING_INDEX 可能在这里工作:

SELECT
    post_content,
    SUBSTRING_INDEX(SUBSTRING_INDEX(post_content, '[table id=', -1), ' ', 1) AS id
FROM wp276_posts
WHERE
    post_content LIKE '%[table%' AND
    post_status = 'publish'   -- you seem to want equivalence here
ORDER BY
    ID;

SUBSTRING_INDEX 的内部调用 returns [table id= 右边 的所有内容。然后外部调用获取 space 的 left 的内容,假定它跟随 id 数字。

是的,您可以使用 SUBSTRING()INSTR() 函数组合:

SELECT SUBSTRING(post_content,INSTR(post_content, 'id=')+3,
                 INSTR(post_content, ' /') - INSTR(post_content, 'id=') - 3
                 ) as substring, ID
  FROM t
 WHERE .....

Demo

对于固定文本,您可以使用 REPLACE

SELECT 
    REPLACE(REPLACE(post_content," /]",""),"[tabelid= ","") as substring
    , ID 
FROM 
    `wp276_posts` 
WHERE 
    `post_content` LIKE '%[table%' 
    AND `post_status` LIKE 'publish' 
ORDER BY `ID` ASC