gpt4 book ai didi

mysql - 为什么 perl DBI 在跟踪语句返回 1 时返回 0 行

转载 作者:行者123 更新时间:2023-11-29 02:28:46 24 4
gpt4 key购买 nike

这周我第一次使用 perl DBI。

大多数查询/插入工作正常,但我遇到了一个返回 0 行的特定查询的问题。当我为 perl DBI 启用跟踪,并将完全相同的语句从跟踪复制到服务器(通过 HeidiSQL)时,返回 1 行。

原始 SQL 查询中是否存在歧义?目的是检索具有最新时间戳的行。时间戳列中没有重复项。

数据库连接的初始设置:

$dsn = 'dbi:mysql:<servername>:<port>';
$dbh = DBI->connect($dsn, "<username>","<password>") or die "unable to connect $DBI::errstr\n";

准备和执行语句:代码到达 print 'no rows found'

my $sth = $dbh->prepare("SELECT name, location, timestamp, notified FROM storage
WHERE name = ? AND location = ?
AND timestamp = (SELECT MAX(timestamp) FROM storage)");

$sth->execute($strg_data->{name}, $strg_data->{location});

my @latest = $sth->fetchrow_array();

if (@latest) {
<snipped>
}
else {
print "no rows found!\n";
}

从 perl DBI 跟踪中提取(级别设置为 2):

 -> prepare for DBD::mysql::db (DBI::db=HASH(0xebe4c0)~0xec0010 'SELECT name, location, timestamp, notified FROM storage
WHERE name = ? AND location= ? AND timestamp = (SELECT MAX(timestamp) FROM storage)')
Setting mysql_use_result to 0
<- prepare= DBI::st=HASH(0xecd7d0) at monitor.pl line 147
-> execute for DBD::mysql::st (DBI::st=HASH(0xecd7d0)~0xec9e50 'xxxx' '/tmp/')
-> dbd_st_execute for 00ecd7a0
-> mysql_st_interal_execute
Binding parameters: SELECT name, location, timestamp, notified FROM storage
WHERE name = 'xxxx' AND location= '/tmp/' AND timestamp = (SELECT MAX(timestamp) FROM storage)
<- mysql_st_internal_execute returning rows 0
<- dbd_st_execute returning imp_sth->row_num 0
<- execute= '0E0' at monitor.pl line 152

最佳答案

SELECT MAX(timestamp) FROM storage 查找最大时间戳而不考虑名称和位置。如果您指定的名称和位置没有带有该时间戳的记录,您将获得 0 行。

您可能需要此查询:

SELECT name, location, timestamp, notified FROM storage
WHERE name = ? AND location = ?
ORDER BY timestamp desc LIMIT 1

关于mysql - 为什么 perl DBI 在跟踪语句返回 1 时返回 0 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16364904/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com