【发布时间】:2015-02-22 09:45:43
【问题描述】:
我有一个表lead_submission,其中包含特定格式的用户值,例如
agent_name qa_details
xxx 1001:|1083:|504:Yes|1009:|
ccc 504:Yes|1083:No|1008:|1009:|
现在我想从两行中获取只说 504:Yes 的计数
这些值来自另一个表paid_response
qno paid_response
504 Yes
1083 No
1083 Possibly
<?php
//db connection goes here
$sql=mysql_query("select qno,paid_response from paid_response where qno='504' ");
while($rows=mysql_fetch_array($sql)) {
$exqnos= $rows['qno'].'|'.$rows['paid_response'];
}
list($key,$val)=explode('|',$exqnos);
$exqno[$key]=$val;
foreach($exqno as $qno=>$value) {
$string .="qa_details LIKE '%|$qno:$value|%' ";
}
$sql=mysql_query("SELECT count(agent_name) as agent_cnt,count($string) as ppicount FROM `lead_submission` WHERE $string "); ?>
<table border="1">
<thead>
<tr>
<th>CountAgent</th>
<th>504-COUNT</th>
</tr>
<?php
while($row=mysql_fetch_array($sql)) { ?>
<tr style="color:red" >
<td><?php echo $row['agent_cnt']; ?></td>
<td><?php echo $row['ppicount']; ?></td>
</tr>
<?php
}
?>
现在通过这样做,我将 504:Yes 计为 2
CountAgent 504-COUNT
2 2 //as u can see that `504:Yes` has occured two times in lead_submission table.
我的意思是,我如何计算另一个组合,例如 1083:No 并在同一张表中显示计数
NB:- cant we just fetch the combination like `504:Yes` or `1083:No` or `1083:Yes` from paid_response table to maintain stability so that i dont have to change the query everytime.
CountAgent 504-COUNT 1083-Count
2 2 1 //how to get this count `1083:No` . as u can see it only appeared 1 times in `lead_submission` table
【问题讨论】:
-
为什么要这样存储数据?如果你能正确地规范化信息,这会更容易,而且通过这种方式来解决这个问题可能是一个更好的主意。
-
你能举个例子吗?
-
'没有这个并发症。'复杂性来自于您没有选择一种好的格式来保存数据。你宁愿标准化你的数据。
-
所以你的意思是我没有办法从这里数数?
-
删除 | 不是一个好主意和 : 。你如何区分问题 504 和答案 504?
标签: php mysql sql select pivot