Search code examples
mysqlcountgroup-concathaving

How to COUNT MySQL results when I use “GROUP_CONCAT” and “HAVING” in the query?


So I have a MySQL query that I want to count. Originally I use wordpress' get_results("") but I also need to count the total in a different query, because in my original query I LIMIT the results to create pages.

So this is my original query, does anyone know how I can count this?

SELECT wpp.ID, post_title, wp_terms.name AS category, wp_terms.slug AS slug, supplier_company, 
    GROUP_CONCAT(wp_terms.slug SEPARATOR ', ') AS allslug,
    GROUP_CONCAT(wp_terms.name SEPARATOR ', ') AS allcatname
    FROM wp_posts AS wpp
    LEFT JOIN wp_term_relationships ON wpp.ID = object_id
    LEFT JOIN wp_terms ON term_taxonomy_id = wp_terms.term_id
    LEFT JOIN wp_teleapo_supplier AS s ON wpp.post_author = s.ID



    /* BASIC SEARCH on normal fields */
    WHERE post_type = 'post' 


    GROUP BY wpp.ID

    /* SEARCH on CONCAT FIELDS*/
    HAVING

    (post_title LIKE '%%'
    OR allcatname LIKE '%%'
    OR allslug LIKE '%%'
    OR supplier_company LIKE '%%')
    AND (allslug LIKE '%health-and-beauty%'
    ) AND (allslug LIKE '%%'
    ) AND 

    /* ADD EXTRA SEARCH TAGS: */
    /* Language tag */
     allslug LIKE '%english%' 
    /* ..... tag */

    /* AND allslug LIKE '%......... %' */
    ORDER BY post_date DESC 

Solution

  • Just retrieve found_rows directly after your SELECT query.

    select found_rows();