I can't get my head around this one!! So I'm seeking help....
For each user I'm trying to sum the types of posts assigned to them.
User Table: crm_users Columns: users_id, users_first, users_last
Posts Table: crm_entities Columns: crm_id, users_id, settype, post
I would like to COUNT() the total posts for a user where settype=draft as well as settype=published, for example:
Name------------Published---------Drafts
John Smith---------15---------------3
Nancy Grace--------11------------- 5
Jay Martin----------7--------------14
I am sure I am making this more difficult than it probably is... or at least I think !
Thanks for any advice!
Because MySQL uses 1 as representation for true
and 0 for false
, you could use:
SELECT
u.users_first,
u.users_last,
SUM(p.settype='published') as Published,
SUM(p.settype='draft') as Drafts
FROM
crem_users u
LEFT JOIN
crm_entities p
ON
u.users_id = p.users_id
GROUP BY
u.users_id
ORDER BY Published DESC;
尝试:
select u.users_first, (select count(*) as qty from crm_entities e where e.settype='draft' and e.users_id = u.users_id) drafts, (select count(*) as qty from crm_entities e where e.settype='published' and e.users_id = u.users_id) published from crm_users u
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.