向SQL查询添加计数

时间:2014-12-01 23:07:15

标签: php mysql sql count

我在mysql数据库上运行以下SQL查询和php输出...

$count0 = $wpdb->get_results("      
   SELECT *
   FROM
   wp_rg_lead
   INNER JOIN wp_rg_lead_detail ON
   wp_rg_lead.id=wp_rg_lead_detail.lead_id
   WHERE wp_rg_lead.form_id = '46'
   AND  cast(date_created as date) >= current_date - interval '7' day
   AND field_number = '18'
   ORDER BY value
");

foreach ( $count0 as $page ) {
   echo $repid_field . ' - ' . $page->form_id . ' -  ' . $page->value .  ' - ' . $page->lead_id . ' - ' . $page->date_created.'<br/>';
}

这很好但我想为它添加一个计数,这样当它输出该行时它会告诉我有多少&#39;值&#39;存在。

我看过COUNT函数,但我不知道在

中添加它的位置

1 个答案:

答案 0 :(得分:1)

$count0 = $wpdb->get_results("      
   SELECT *, COUNT(*) as TotalValueCount 
   FROM
   wp_rg_lead
   INNER JOIN wp_rg_lead_detail ON
   wp_rg_lead.id=wp_rg_lead_detail.lead_id
   WHERE wp_rg_lead.form_id = '46'
   AND  cast(date_created as date) >= current_date - interval '7' day
   AND field_number = '18'
   ORDER BY value
");