简体   繁体   English

在Wordpress中的SQL语句中的RTRIM

[英]RTRIM in a sql statement in wordpress

I'm trying to check a filename in a sql statement, that I'm using in PHP, in a wordpress template I'm building. 我正在尝试在正在构建的wordpress模板中检查在PHP中使用的sql语句中的文件名。

Bascially I'm trying to use LTRIM to only bring back images that have _a on the end. 基本上,我正在尝试使用LTRIM只带回结尾有_a图像。

eg I have a bunch of images in a series of folders 例如,我在一系列文件夹中有一堆图像

  • clientone_a.jpg
  • clienttwo_b.jpg
  • ...etc... ...等等...

this is what Im using at the moment but it doesnt work; 这是我目前使用的功能,但是没有用;

$query = "
    SELECT F.field_value, P.alttext, P.filename, P.description, p.galleryid, 
        G.path 
    FROM wp_nggcf_field_values F, 
        wp_ngg_pictures P, 
        wp_ngg_gallery G 
    WHERE F.fid=1 
        AND (F.field_value='".$data."') 
        AND F.pid = P.PID 
        AND P.galleryid = G.gid 
        AND LTRIM(P.filename,'_') = '_a.jpg'
";

$caseStudiesResults = $wpdb->get_results($query);

It's just the last LTRIM(P.filename,'_') = '_a.jpg' that is the problem, without that it bring back all the images, and I'm after bringing back the _a one only. 这只是问题的最后一个LTRIM(P.filename,'_') = '_a.jpg' ,如果没有,它将带回所有图像,而我只带回_a

Any ideas on where I'm going wrong ? 对我要去哪里出错有任何想法吗?

Thanks 谢谢

The problem is that you're using the wrong function. 问题在于您使用了错误的功能。 LTRIM() simply removes leading spaces. LTRIM()只是删除前导空格。 What you're looking for is SUBSTRING_INDEX() 您正在寻找的是SUBSTRING_INDEX()

AND SUBSTRING_INDEX(P.filename, '_', -1) = 'a.jpg'

The -1 tells it to return everything to the right of the final delimiter ( -2 would be everything to the right of the 2nd to last delimiter, 1 would be everything to the left of the first, etc)... -1告诉它返回最后一个定界符右边的所有内容( -2将是倒数第二个定界符右边的所有内容, 1将是第一个定界符右边的所有内容,等等)...

Edit: As far as testing it, you can test string functions by doing this: 编辑:就测试而言,您可以通过执行以下操作来测试字符串函数:

SELECT SUBSTRING_INDEX('test_a.jpg', '_', -1) = 'a.jpg'

Run that, and see if it works (it did for me). 运行它,看看它是否有效(对我有用)。 By putting static values, you can try it out without needing to run the whole big query... 通过放置静态值,您可以尝试而无需运行整个大型查询...

替代substring_index

select substring('clientone_a.jpg', -6)='_a.jpg';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM