2011-01-26 83 views
0

我有以下訂購加入

tophits
身份證,戶口兩個MySQL表,時間

topsites
ID,名稱,電子郵件,網址

我的頂端id是1(這是我的tophits.account)。我想選擇名稱和網址從頂級站點和COUNT有多少條記錄存在於一天的帳戶= 1。

因此,他們將所有的回聲出和以往任何時候都佔了點擊率最高的將是在頂部和等等等等

這是笨,我試過..

$this->db->select('topsites.name, topsites.url, topsites.id, COUNT(tophits.account) as hit_count',FALSE); 
    $this->db->from('topsites'); 
    $this->db->where('active', '0'); 
    $this->db->join('tophits', 'tophits.account = topsites.id', 'left'); 
    $this->db->like('tophits.time', '2011-01-25'); 
    $this->db->order_by('hit_count'); 
    $this->db->limit(10); 

但只有它打印出一條記錄,裏面有不止一個......任何人都知道這個問題?

回答

0

添加group by

您使用aggregate功能count

0

您需要在tophits表使用group by

$this->db->select('topsites.name, topsites.url, topsites.id, COUNT(tophits.account) as hit_count',FALSE); 
    $this->db->from('topsites'); 
    $this->db->where('active', '0'); 
    $this->db->join('tophits', 'tophits.account = topsites.id', 'left'); 
    $this->db->group_by('tophits', 'id'); 
    $this->db->like('tophits.time', '2011-01-25'); 
    $this->db->order_by('hit_count'); 
    $this->db->limit(10); 
3

您需要GROUP BY賬戶(對多個賬戶),而且您還需要通過ORDER BY「desc」來查看最熱門的點擊

$this->db->select('topsites.name, topsites.url, topsites.id, COUNT(tophits.account) as hit_count',FALSE); 
$this->db->from('topsites'); 
$this->db->where('active', '0'); 
$this->db->join('tophits', 'tophits.account = topsites.id', 'left'); 
$this->db->like('tophits.time', '2011-01-25'); 
$this->db->group_by('topsites.name, topsites.url, topsites.id'); 
$this->db->order_by('hit_count', 'desc'); 
$this->db->limit(10);