I'm having some trouble with a MySQL query.
The query is pulling from two tables. Both tables contain data, but I only want a count(*) from the second table.
SELECT m.*, (SELECT COUNT(*) FROM cd_members_offers WHERE mo.mo_member_id = m.m_id) as mo_count
FROM cd_members m, cd_members_offers mo
/* these are variables and don't always exist */
WHERE (m.m_fname LIKE '%Richie%' OR m.m_lname LIKE '%Richie%' OR m.m_email LIKE '%Richie%')
AND (m.m_signupdate >= 20130807 AND m.m_signupdate <= 20130810)
AND m.m_add_state = 'QLD'
AND (mo_count >= '2' AND mo_count <= '5')
/* this is the end of the variables */
AND mo.mo_member_id = m.m_id
ORDER BY m.m_id ASC
The /* quotes */ don't actually exist in the code. I just wanted to show how the query could potentially look if all variables are entered.
I had the issue at first where many duplicates would appear. I'm also quite confident that the mo_count >= is not the correct way to do it.
I'm still playing with the code and doing even more research, but if someone is able to save me some hours, I'd really appreciate it.