2

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.

4

2 回答 2

0

尝试这个

        SELECT m.*, (SELECT COUNT(*) FROM cd_members_offers mo WHERE mo.mo_member_id = m.m_id) as mo_count 
        FROM cd_members m
        /* 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 */
        ORDER BY m.m_id ASC

或者可以将 Group By 与 having 子句一起使用

        SELECT m.m_id, count(mo.mo_member_id)
        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' 
        /* this is the end of the variables */
        AND mo.mo_member_id = m.m_id 
        Group by m.m_id
        having count(mo.mo_member_id) between 2 and 5
        ORDER BY m.m_id ASC
于 2013-08-08T06:13:48.847 回答
0

很难确定没有看到您的表架构、示例数据等,但您的查询可能看起来像这样

SELECT m.*, COUNT(mo_member_id) mo_count 
  FROM cd_members m LEFT JOIN cd_members_offers mo 
    ON mo.mo_member_id = m.m_id 
 WHERE (m.m_fname LIKE '%Richie%' OR m.m_lname LIKE '%Richie%' OR m.m_email LIKE '%Richie%')
   AND m.m_signupdate >= '2013-08-07' AND m.m_signupdate <= '2013-08-10'
   AND m.m_add_state = 'QLD' 
 GROUP BY m.m_id
HAVING mo_count BETWEEN 0 AND 5

样本输出:

| M_ID | M_FNAME | M_LNAME | 邮箱 | M_SIGNUPDATE | M_ADD_STATE | MO_COUNT |
-------------------------------------------------- -------------------------------------------------- ----
| 1 | 里奇 | 里奇 | richie@mail.com | 2013 年 8 月 8 日 00:00:00+0000 | 昆士兰 | 3 |
| 2 | 里奇2 | 里奇2 | richie2@mail.com | 2013 年 8 月 9 日 00:00:00+0000 | 昆士兰 | 0 |

这是SQLFiddle演示

于 2013-08-08T06:20:16.083 回答