I am trying to write a query that can count a joined tables and specific column values but I can’t wrap my head around how that would work.
My knowledge is limited to thinking if I want to count all, it would look like this.
"SELECT COUNT(*) FROM 'table'";
So this essentially will return the count of all the rows in that specific table. But I need specific rows in specific tables so how can I do the same count for that?
For example I have this.
SELECT u.ID AS id, u.user_login AS login, u.user_email AS email
, m_firstname.meta_value AS first_name
FROM users u
LEFT JOIN usermeta m_firstname ON m_firstname.user_id = u.ID
AND m_firstname.meta_key = 'first_name'
From the above statement how can I count it other than looping it through an array? Is there a MYSQL syntax I can use to resolve this?
*Above example code is just to illustrate what I am trying to do and may not be fully working code.
If you want to count all users, you could use
COUNT(1)here. If you want to count the number ofNON-NULLvalues for a specific column, useCOUNT(columnName)The above query will count all records that match this query where
m_firstName.user_idisNOT NULL