简体   繁体   English

Perl脚本还是MySQL修复?

[英]Perl script or MySQL fix?

I am a beginner in Perl and just got a tweak task with a perl scipt. 我是Perl的初学者,只是使用perl scipt进行调整任务。 The statement that I am focusing now is: 我现在关注的声明是:

my $sth = $dbh->prepare('SELECT StringValue FROM CustomData WHERE (Record_ID = \'' . $ref->{'Record_ID'} . '\' && Field_ID = \'' . $metadata[11] . '\') LIMIT 1;');

The current statement will pull every record that matches the Record_ID value. 当前语句将拉出与Record_ID值匹配的每条记录。 However, it needs to be changed to only pull records where Record_ID begins with digit 1,2,9 . 但是,需要将其更改为仅拉取Record_ID以数字1,2,9开头的记录

I am thinking this is more like a regular expression issue, is that correct? 我认为这更像是正则表达式问题,这是正确的吗? If that is case, I should only be modifying the 如果是这种情况,我应该只修改

 Record_ID = \'' . $ref->{'Record_ID'}

part. 部分。 Is that correct? 那是对的吗? Or that should be something to be fixed in the prepare statement? 或者那应该是prepare声明中要修复的内容?

You should use placeholders instead of trying to interpolate your variables and try to do your own quoting. 您应该使用占位符而不是尝试插入变量并尝试进行自己的引用。 You might even consider using placeholders for the '1%' etc, unless you consider them static throughout all your queries. 您甚至可以考虑使用'1%'等占位符,除非您在所有查询中都认为它们是静态的。

my $sth = $dbh->prepare( q#
     SELECT StringValue FROM CustomData 
     WHERE (Record_ID = ? && Field_ID = ?) 
     AND (Record_ID LIKE '1%' OR Record_ID LIKE '2%' OR Record_ID LIKE '9%')
     LIMIT 1
#);

$sth->execute($ref->{'Record_ID'}, $metadata[11]);

Add an AND -part in your WHERE clause to filter the unwanted Record_IDs. 在WHERE子句中添加AND -part以过滤不需要的Record_ID。

SELECT StringValue 
FROM CustomData \
WHERE (Record_ID = \'' . $ref->{'Record_ID'} . '\' && Field_ID = \'' . $metadata[11] . '\') 
AND (Record_ID  LIKE "1%" OR Record_ID  LIKE "2%" OR Record_ID  LIKE "9%")
LIMIT 1

To avoid masking the ' so many times you can use qq 为了避免掩盖'这么多次,你可以使用qq

my $sth = $dbh->prepare( qq§SELECT StringValue FROM CustomData 
                            WHERE (Record_ID = '$ref->{Record_ID}' AND 
                                   Field_ID = '$metadata[11]') LIMIT 1§
                       ) ;

You are right about changing the part Record_ID = \\'' . $ref->{'Record_ID'} . '\\' 你是正确的改变部分Record_ID = \\'' . $ref->{'Record_ID'} . '\\' Record_ID = \\'' . $ref->{'Record_ID'} . '\\'

replace it with Record_ID LIKE \\'1%\\' || Record_ID LIKE \\'2%\\' || Record_ID LIKE \\'9%\\' Record_ID LIKE \\'1%\\' || Record_ID LIKE \\'2%\\' || Record_ID LIKE \\'9%\\'替换它 Record_ID LIKE \\'1%\\' || Record_ID LIKE \\'2%\\' || Record_ID LIKE \\'9%\\'

Remove LIMIT 1 ,this only matches any one row,either of starting with 1,2 or 9 删除LIMIT 1 ,这只匹配任何一行,从1,2或9开始

Here's how I think solution could go 以下是我认为解决方案可以实现的方式

my $sth = $dbh->prepare('
    SELECT StringValue 
    FROM CustomData 
    WHERE ( Record_ID LIKE \'1%\' || Record_ID LIKE \'2%\' || Record_ID LIKE \'9%\' 
    && Field_ID = \''. $metadata . '\');');

$sth->execute;

while (my @arr=$sth->fetchrow_array())
   {
   print @arr;
   }

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

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