此sql语句引发错误

时间:2014-08-19 15:43:57

标签: php mysql sql

我正在尝试使用以下查询从表中获取计数数据

SELECT courses.published, courses.archived, courses.draft from
 (  select count(*) as published from courses where published = 't' union all
    select count(*) as draft from courses where draft = 't' union all
    select count(*) as archived from courses where archived = 't'
 ) as courses

我想要一张能够做到这一点的桌子

$result['courses']['published']
$result['courses']['draft']
$result['courses']['archived']

但查询引发了错误

#1054 - Unknown column 'courses.archived' in 'field list'

1 个答案:

答案 0 :(得分:3)

这个怎么样:

select p.published, d.draft, a.archived
from
( select count(*) as published from courses where published = 't' ) p
cross join
( select count(*) as draft from courses where draft = 't' ) d
cross join 
( select count(*) as archived from courses where archived = 't' ) a