【问题标题】:Mysql GROUP_BY issue (active record)Mysql GROUP BY 问题(活动记录)
【发布时间】:2010-12-11 22:47:27
【问题描述】:

我在下面的查询中尝试执行的操作:发布的问题属于 1 个大陆和 1 个国家/地区。用户发布这些问题的答案。在下面的查询中,我得到了所有数据和答案数量

问题是 COUNT(answers.text) 在实际应该为零时返回数字 1。这是为了防止问题没有答案。

长话短说,我不确定应该如何使用 GROUP_BY,我觉得这就是问题所在。谁能解释一下?


$this->db->select('questions.id, users.username, url_title, title, questions.text, questions.date_posted,continents.full_name ascontinent, countries.full_name as country');

$this->db->select("COUNT('answers.text') as nbr_answers");

$this->db->join('continents', 'questions.loc_continent =continents.id');

$this->db->join('countries', 'questions.loc_country = countries.id');

$this->db->join('answers', 'answers.q_fid = questions.id', 'left');

$this->db->join('users', 'users.id = answers.user_id_fk', 'left');

$this->db->order_by('questions.id','desc');

$this->db->group_by('url_title');

【问题讨论】:

    标签: mysql


    【解决方案1】:

    你有

    COUNT('answers.text') 
    

    应该是什么时候

    COUNT(`answers.text`) 
    

    【讨论】:

    • 天哪,速度真快。你是明星!!
    猜你喜欢
    • 2012-02-17
    • 2016-01-27
    • 1970-01-01
    • 2010-11-10
    • 2019-02-11
    • 1970-01-01
    • 1970-01-01
    • 2016-11-23
    • 2017-10-20
    相关资源
    最近更新 更多