遇到mysql计数问题

时间:2015-06-16 12:24:27

标签: php mysql

执行查询后出现此错误

  

1064 - 您的SQL语法出错;检查与MySQL服务器版本对应的手册,以获得正确的语法   在'*)附近作为内容的总内容作为c内连接类别为   cat on cat.id ='at line 1

我的sql喜欢这个

select z.name as zone_name, COUNT(c.*) as totalcontents 
from contents as c 
inner join categories as cat 
ON cat.id = c.category_id 
inner join zones as z 
ON z.id = cat.zone_id where c.created_by = 14 group by z.id

2 个答案:

答案 0 :(得分:0)

当您使用INNER JOIN时,COUNT(*)可以使用。

select z.name as zone_name, COUNT(*) as totalcontents  ....

OR

select z.name as zone_name, COUNT(z.name) as totalcontents  ....

Go through this

答案 1 :(得分:0)

当您使用group by function指定列显示列其他列以显示给定错误时。我想你试试这个

select z.name as zone_name, COUNT(z.name) as totalcontents 
from contents as c 
inner join categories as cat 
ON cat.id = c.category_id 
inner join zones as z 
ON z.id = cat.zone_id where c.created_by = 14 group by z.name

select z.name as zone_name,totalcontents 
from zones as z
join (select z.id as id, COUNT(z.id) as totalcontents 
    from contents as c 
    inner join categories as cat 
    ON cat.id = c.category_id 
    inner join zones as z 
    ON z.id = cat.zone_id where c.created_by = 14 group by z.id) a
ON z.id = a.id