简体   繁体   中英

This sql statement throws an error

I am trying to get count data from the table using the following query

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

I want a table where i can do this

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

but the query threw an error

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

How about this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM