简体   繁体   中英

MySQL COUNT() not returning total number it is counting each rows separately

I am having some problem with my MySQL query and I need some guidance here. I am creating a search and I want to know how many item was found in my search with my query. But the problem is, my query returning count for each rows separately, It's not returning total count. Here what I have right now.

My query

SELECT
    COUNT(t.id) AS total
FROM
    trouble t
LEFT JOIN district d ON d.id = t.district
LEFT JOIN country c ON c.id = t.country
LEFT JOIN multi_category mc ON mc.t_id = t.id
LEFT JOIN category ct ON ct.id = mc.ct_id
LEFT JOIN state s ON s.id = t.state
WHERE
    t.name      LIKE '%keyword%' OR
    t.title     LIKE '%keyword%' OR
    t.tags      LIKE '%keyword%' OR
    ct.category LIKE '%keyword%' OR
    c.country   LIKE '%keyword%' OR
    s.state     LIKE '%keyword%' OR
    d.district  LIKE '%keyword%'
GROUP BY
    t.id

I have 14 rows in my database with the keyword I am searching with and this query returning 14 rows, here is a snap.

mysql数据集

But it is counting each rows id's individually. I don't want that. What I want is, I want the total row count which is 14 and I want it in a single row. Can you please help me achieve this?

Thanks in advance.

First get the unique number of IDs and then return the total number of IDs.

SELECT count(*) AS Total
FROM 
( 
    SELECT DISTINCT t.id
    FROM trouble t
      LEFT JOIN district d ON d.id = t.district
      LEFT JOIN country c ON c.id = t.country
      LEFT JOIN multi_category mc ON mc.t_id = t.id
      LEFT JOIN category ct ON ct.id = mc.ct_id
      LEFT JOIN state s ON s.id = t.state
      WHERE
          t.name LIKE '%keyword%' OR
          t.title LIKE '%keyword%' OR
          t.tags LIKE '%keyword%' OR
          ct.category LIKE '%keyword%' OR
          c.country LIKE '%keyword%' OR
          s.state LIKE '%keyword%' OR
          d.district LIKE '%keyword%'
      GROUP BY t.id
) resultTable

Use exists instead of LEFT JOIN s. The joins just multiply rows when there are multiple matches:

SELECT COUNT(*)
FROM trouble t
WHERE t.name LIKE '%keyword%' OR
      t.title LIKE '%keyword%' OR
      t.tags LIKE '%keyword%' OR
      EXISTS (SELECT 1
              FROM district d
              WHERE d.id = t.district AND d.district LIKE '%keyword%'
             ) OR
      EXISTS (SELECT 1
              FROM country c
              WHERE c.id = t.country AND c.country LIKE '%keyword%'
             ) OR
      EXISTS (SELECT 1
              FROM state s
              WHERE s.id = t.state AND s.state LIKE '%keyword%'
             ) OR
      EXISTS (SELECT 1
              FROM multi_category mc JOIN 
                   category ct
                   ON ct.id = mc.ct_id 
              WHERE mc.t_id = t.id AND
                    (ct.category LIKE '%keyword%' OR
                     c.country LIKE '%keyword%'
                    )
             );

By eliminating the creation of multiple rows, this should also be faster.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM