简体   繁体   English

为什么查询返回的结果是列而不是行?

[英]Why is my query returning results in column instead of rows?

Have a working SQL statement: 有一个有效的SQL语句:

my $q_it = $dbh->prepare("SELECT customdata.Field_ID,
    customdata.Record_ID,
    customdata.StringValue
    FROM customdata
    WHERE customdata.Field_ID='10012' && (StringValue LIKE '1%' OR StringValue LIKE   '2%' OR StringValue LIKE '9%');
   ");

I have written a very simple Perl script for my client to run on their server/db. 我为客户端编写了一个非常简单的Perl脚本,以便在其服务器/数据库上运行。 I could not test it directly, but I passed my code to their DBA: 我无法直接对其进行测试,但是我将代码传递给了他们的DBA:

  $q_it->execute();
   open (MYFILE, '>>data.txt');
    while (my @row=$q_it->fetchrow_array)
    {
       print MYFILE $row[0].$row[1].$row[2];
    }
   close (MYFILE);

$q_it is just a normal SQL Select statement. $ q_it只是普通的SQL Select语句。 I would assume the data.txt will contain many records(rows). 我假设data.txt将包含许多记录(行)。 However, surprisingly, it return the results in a single column, but many rows like: 但是,令人惊讶的是,它在单个列中返回结果,但是却有许多行,例如:

      100012
      100012
       ...
      100012
      315941
      315667
       ...
      315633 
      2011-06
      2011-06
       ...
      2011-06

There are just about correct no. 大约有正确的编号。 of rows for "100012", the "31"values and the date strings. “ 100012”,“ 31”值和日期字符串的行数。 Ideally, it should be 理想情况下,应该

100012  315941 2011-06
100012  315667 2011-06
100012  315633 2011-06

Could it be something I did wrong in my Perl or is this because their MySQL database has different structures? 难道是我在Perl中做错了什么,还是因为他们的MySQL数据库具有不同的结构?

Thanks for the help! 谢谢您的帮助!

I would guess that you are looking at a previous attempt to dump the database. 我猜您正在查看以前的转储数据库尝试。 To get all values of the first column, followed by all values of the second etc. requires a very different program from the one you have shown. 要获取第一列的所有值,然后获取第二列的所有值,等等,需要与所显示的程序完全不同的程序。

Don't forget that you are opening the file for append , which will leave any old data at the start of the file. 不要忘记您正在打开文件append ,这会将所有旧数据保留在文件的开头。 I would have thought an open for write would be appropriate here, as the output from failed attempts is of little value. 我本来以为可以进行开放写操作 ,因为失败尝试的输出几乎没有价值。

I would also check the status of the open using 我还要检查的状态open使用

open MYFILE, '>', 'data.txt' or die $!;

Apart from that, unless you have set $\\ to a newline, you need to terminate your printed output with a newline to separate the records. 除此之外,除非您将$\\设置$\\换行符,否则需要用换行符终止打印输出以分隔记录。 It is also easier to write 写起来也比较容易

print "@row\n";

rather than mention each of the fields explicity. 而不是明确提及每个字段。

I really like using something like this: 我真的很喜欢使用这样的东西:

sub fetch_result_rows {
    my ($dbh, $sql, @bind_params) = @_; 
    try {
        return $dbh->selectall_arrayref($sql, { RaiseError => 1, Slice => {}, }, @bind_params);
    } catch {
        confess("Unable to run SQL:\n$sql\nBIND PARAMS: @{[ join(', ', @bind_params) ]}");
    }   
}

The magic is in Slice, which causes the return to come back as an arrayref of hash refs. 魔术存在于Slice中,这使得返回值以哈希引用的arrayref形式返回。 To me the performance overhead is worth it because the result looks like: 对我来说,性能开销是值得的,因为结果如下所示:

[
{ column1 => value, column2 => value, column3 => value }, # row1
{ column1 => value, column2 => value, column3 => value }, # row2
...

]

Read up in perldoc DBI for more information 在perldoc DBI中阅读以获取更多信息

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

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