SQL查询:获取具有最大编号的字符串|解析?

时间:2017-03-12 17:35:04

标签: php mysql string wordpress advanced-custom-fields

我有一个如下所示的数据库: enter image description here

不要问我为什么,但这是由WordPress插件编写的,所以我必须处理它。我想要的是获得meta_key = product_shops_X_link的每个链接。但是每个帖子都有不同数量的产品商店链接。

在这张照片中,我尝试了Where meta_key Like "%_link%"。但是,如果我这样做一个查询,我也会得到product_shops_X_link字段在_之前有product的所有meta_keys ...

我想要的只是所有product_shops_X_link字段。现在我有两种方式。

方式1:获取最高数量的元键字段Where meta_key Like "%product_shops_X_link%" OR meta_key Like "%_product_shops_X_link%"_productproduct并不重要。数字相等。如果只有product_shops_0_link字段,则必须还有_product_shops_0_link字段。

方式2:创建一个模式,该模式仅搜索所有product_shops_X_link字段,而不包含_product的开头,并为我提供所有product_shops_X_link字段。无论该数字包含哪个数字。

但在这两种情况下我都不知道如何编码...... 有谁知道如何编码或者是否有更好的方法?

问候,谢谢!

3 个答案:

答案 0 :(得分:0)

如果您不希望以_product开头的条目,则可以从_模式中排除%LIKE,例如:

SELECT *
FROM links
WHERE meta_key LIKE 'product_%';

答案 1 :(得分:0)

很抱歉,如果我误解了,但您是否想从这些行中捕获数字?如果是这样,您可以使用本机REPLACE()函数,如下所示:

SELECT *, REPLACE(REPLACE(meta_key, 'product_shops_', ''),'_link','') as captured_number
FROM mytable
WHERE meta_key LIKE 'product\_shops\_%\_link'
ORDER BY captured_number DESC

这样可以获得您想要的行数最多的行。或者,您可以将this regex library用于mysql,然后使用正则表达式捕获数字:

SELECT *, PREG_CAPTURE( '/product_shops_([0-9]+)/', meta_key) as captured_number
FROM mytable
WHERE meta_key LIKE 'product\_shops\_%\_link'
ORDER BY captured_number DESC

答案 2 :(得分:0)

  

但是如何为查询添加更多属性,如post_id或   meta_key

这些内容可以与GHCi> runEffect $ (src //> lhs) >>~ requestForever rhs rhs 0: Will this happen for every value? rhs 1: response nr. 1 lhs: yield manually to upstream! rhs 2: response nr. 2 lhs: return to upstream rhs 0: Will this happen for every value? rhs 1: response nr. 3 lhs: yield manually to upstream! rhs 2: response nr. 4 lhs: return to upstream rhs 0: Will this happen for every value? rhs 1: response nr. 5 lhs: yield manually to upstream! rhs 2: response nr. 6 lhs: return to upstream rhs 0: Will this happen for every value? rhs 1: response nr. 7 lhs: yield manually to upstream! rhs 2: response nr. 8 lhs: return to upstream rhs 0: Will this happen for every value? rhs 1: response nr. 9 lhs: yield manually to upstream! rhs 2: response nr. 10 lhs: return to upstream 合并,例如查询ANDpost_id = 21179meta_key开头的项目:

product_..