【问题标题】:MySQL SubString to extract exact partMySQL SubString 提取精确部分
【发布时间】:2018-08-16 22:23:14
【问题描述】:

对于我缺乏 SQL 知识,我有一个挑战。

我的数据库中有一个列,我将其保存在其中:

<div class="product-cover" style="border:none;">
<div class="images-container">
<div class="images-container"><img src="https://myurl.com/img/cms/_DSC6641.jpg" title="" alt="" /></div>
</div>

我正在尝试提取每行都不同的字符串“https://myurl.com/img/cms/_DSC6641.jpg”。

是否可以创建一个查询来提取 src="..." 之间的确切部分?

我正在使用 MySQL/MariaDB

【问题讨论】:

  • 为什么要用mysql,你用的是哪种后端语言?
  • 你的mysql版本是多少?
  • 提供铲子为什么要用勺子挖洞?
  • 为什么不使用 SQL 迭代,因为它已经运行了?它将为我节省一些数据解析,稍后...

标签: mysql sql regex substring mariadb


【解决方案1】:

我会使用substring_index():

select substring_index(substring_index(col, '<div class="images-container"><img src=', -1), ' ', 1)

【讨论】:

  • OP 说他们有多个图像并想提取所有图像
  • @smit 。 . .我根本没有在问题中看到这一点:“我正在尝试提取每行都不同的字符串“myurl.com/img/cms/_DSC6641.jpg”。””
【解决方案2】:

我找到了使用D-Shih回复的方法

SELECT 
	SUBSTRING( 
		pl.description,
		LOCATE('src="',pl.description) + CHAR_LENGTH('src="'),
		( LOCATE('" title', pl.description)) - ( LOCATE('src="', pl.description) + CHAR_LENGTH('src="') )
	) as url
FROM ps_product p 
INNER JOIN ps_product_lang pl ON (pl.id_product=p.id_product)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多