I am trying to get a number of users with role - subscriber, number of two post types and a meta value from a post by using the SQL query:
SELECT
SUM(CASE WHEN meta_key='wp_capabilities' AND meta_value LIKE '%subscriber%' THEN 1 ELSE 0 END) AS users,
SUM(CASE WHEN post_type='post' AND post_status='publish' THEN 1 ELSE 0 END) AS posts,
SUM(CASE WHEN post_type='updates' AND post_status='publish' THEN 1 ELSE 0 END) AS updates, meta_value AS version
FROM wp_usermeta,wp_posts,wp_postmeta WHERE meta_key='content_version' AND post_id=1
There is no results and the error displayed "#1052 - Column 'meta_key' in field list is ambiguous"
Can anybody please help what I am doing wrong?
The results view I want to get should be like:
| users | posts | updates | version |
-------------------------------------------
| 2 | 5 | 2 | 1 |
Thank you
UPDATE:
When I updated the query with table aliases:
SELECT
SUM(CASE WHEN a.meta_key='wp_capabilities' AND a.meta_value LIKE '%subscriber%' THEN 1 ELSE 0 END) AS users,
SUM(CASE WHEN b.post_type='post' AND b.post_status='publish' THEN 1 ELSE 0 END) AS posts,
SUM(CASE WHEN b.post_type='updates' AND b.post_status='publish' THEN 1 ELSE 0 END) AS updates, c.meta_value AS version
FROM wp_usermeta AS a, wp_posts AS b, wp_postmeta AS c WHERE c.meta_key='content_version' AND post_id=1
It is bringing wrong results, seems SUM is calculating every record in table. Has any one had this issue before? I am using 5.5.44-MariaDB