首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >模板工具包显示唯一的行。

模板工具包显示唯一的行。
EN

Stack Overflow用户
提问于 2017-09-17 15:31:56
回答 1查看 165关注 0票数 8

我使用Perl dancer2编写应用程序。在mysql中运行select查询将显示所有记录。但是,在dancer2和模板工具箱中运行的相同查询只显示唯一的记录。

例如:在mysql客户端中运行时获取34条记录。

代码语言:javascript
复制
    select timing.time as Time,
           church.church_name as Church_Name, 
           church.church_address as Address, 
           language.language_name as Language, 
           denomination.denomination_name as Denomination, 
           city.city_name as City, 
           state.state_name as State, 
           country.country_name as Country 
      from church 
      join country on church.church_countryid=country_id 
      join state on church.church_stateid=state.state_id 
      join city on church.church_cityid=city.city_id 
      join church_mass_timing on church.church_id=church_mass_timing.church_id 
      join timing on church_mass_timing.time_id=timing.time_id 
      join language on church_mass_timing.language_id=language.language_id 
      join denomination on church.church_denominationid=denomination.denomination_id 
  order by church.church_name,
           timing.time;

模板工具包中的同一个查询返回11条记录。

代码语言:javascript
复制
get '/church_list' => sub {
my $db = connect_db();
my $sql='select timing.time as Time,
                church.church_name as Church_Name, 
                church.church_address as Address, 
                language.language_name as Language, 
                denomination.denomination_name as Denomination, 
                city.city_name as City, 
                state.state_name as State, 
                country.country_name as Country 
           from church 
           join country on church.church_countryid=country_id 
           join state on church.church_stateid=state.state_id 
           join city on church.church_cityid=city.city_id 
           join church_mass_timing on church.church_id=church_mass_timing.church_id 
           join timing on church_mass_timing.time_id=timing.time_id 
           join language on church_mass_timing.language_id=language.language_id 
           join denomination on church.church_denominationid=denomination.denomination_id 
       order by church.church_name,
                timing.time';
my $sth = $db->prepare($sql) or die $db->errstr;
$sth->execute or die $sth->errstr;
template 'church_list.tt' => { 'title' => 'church list',
   'site' => 'Church Timings', 
   'entries' => $sth->fetchall_hashref('Time'),
};
};    

这是church_list.tt

代码语言:javascript
复制
<% INCLUDE header.tt %>
<ul id="content">
<button id="btnExport">Export to xls</button>
<br />
<br />
<div id="table_wrapper">

<% IF entries.size %>
<table border="1" cellspacing="2" widht="100%">
<tr>
<th><center>Time</center></th>
<th><center>Church name</center></th>
<th><center>Address</center></th>
<th><center>Language</center></th>
<th><center>Denomination</center></th>  
<th><center>City</center></th>
<th><center>State</center></th>
<th><center>Country</center></th>
</tr>
<% FOREACH id IN entries.keys %>
<tr>
<td><% entries.$id.Time %></td>
<td><% entries.$id.Church_Name %></td>
<td><% entries.$id.Address %></td>
<td><% entries.$id.language %></td>
<td><% entries.$id.denomination %></td>
<td><% entries.$id.city %></td>
<td><% entries.$id.state %></td>
<td><% entries.$id.country %></td>
</tr>
<% END %>
</table>
</div>
<% ELSE %>
<li><em>No entries here so far.</em>
<% END %>
</ul>

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-17 16:36:41

请注意哈希瑞夫返回hashref { FieldValue => FieldsHash }。

当某些字段值不是唯一的时,您会遗漏一些FieldHashes (下一个具有相同时间值的记录将覆盖以前的结果散列)。

在这些情况下,请使用行数组(selectall_arrayref)。

P.S.:这个查询中的时间总是不唯一的(有时可以同时添加两个记录)。

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46265790

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档