简体   繁体   中英

SQL SELECT showing records only if the number of records is greater than N

I have a table defined like this (MySQL 5.1):

CREATE TABLE mysql_test_a ( 
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
firstname VARCHAR(30) NOT NULL, 
lastname VARCHAR(30) NOT NULL,  
email VARCHAR(50), 
reg_date TIMESTAMP 
); 

Sample dataset:

INSERT INTO `mysql_test_a` (`id`, `firstname`, `lastname`, `email`, `reg_date`) VALUES ('1', 'Marcello', 'Santucci', 'marcello@tux.net', CURRENT_TIMESTAMP); 
INSERT INTO `mysql_test_a` (`id`, `firstname`, `lastname`, `email`, `reg_date`) VALUES ('2', 'Mike', 'Santucci', 'mike@tux.net', CURRENT_TIMESTAMP); 
INSERT INTO `mysql_test_a` (`id`, `firstname`, `lastname`, `email`, `reg_date`) VALUES ('3', 'Anna Maria', 'Gabriele', 'anna.maria@gabriele.net', CURRENT_TIMESTAMP); 
INSERT INTO `mysql_test_a` (`id`, `firstname`, `lastname`, `email`, `reg_date`) VALUES ('4', 'Matilde Josefa', 'Santucci', 'matilde.josefa@tux.net', CURRENT_TIMESTAMP); 
INSERT INTO `mysql_test_a` (`id`, `firstname`, `lastname`, `email`, `reg_date`) VALUES ('5', 'Milena', 'Santucci', 'mile@tux.net', CURRENT_TIMESTAMP); 
INSERT INTO `mysql_test_a` (`id`, `firstname`, `lastname`, `email`, `reg_date`) VALUES ('6', 'Luca', 'Pensa', 'luca@pensa.net', CURRENT_TIMESTAMP); 
INSERT INTO `mysql_test_a` (`id`, `firstname`, `lastname`, `email`, `reg_date`) VALUES ('7', 'Lorenzo', 'Pensa', 'lo@pensa.net', CURRENT_TIMESTAMP); 

I need to show records matching a certain criteria (lets suppose lastname = 'Santucci' ) only if the number of records is greater than a certain defined limit (lets say 2). I tried in various way without success the most promising form was:

SELECT
    id,
    firstname,
    lastname
FROM
    mysql_test_a
WHERE
    lastname = 'Santucci'
HAVING COUNT(*) > 2

It returns only the first record.

I would prefer to use something like this form because HAVING clause will enable the use of a parameter.

--- LATE UPDATE ---

I have to be more specific on the solution: I'm looking for something that do not deal with the inner SELECT and more specifically its WHERE clause because, as I pointed out, the one provided is pretty hypotetical (ie it can be quite different from this and much more complex). Of course I appreciate any other hint.

You can use the sub-query in your query as follows:

SELECT id, firstname, lastname
FROM mysql_test_a a
WHERE lastname = 'Santucci'
  and (select count(1) from mysql_test_a b where b.lastname  = a.lastname) > 2

You may be looking for this:

SELECT 
 *
 FROM
    (SELECT 
        id, 
        firstname, 
        lastname
    FROM
        mysql_test_a
    WHERE
        lastname = 'Santucci') a,
    (SELECT 
        id, 
        firstname, 
        lastname
    FROM
        mysql_test_a
    WHERE
        lastname = 'Santucci'
    HAVING COUNT(*) > 2) b
WHERE
    a.lastname = b.lastname

I am guessing that your result is

1 Marcello Santucci

but you want something like this:

1   Marcello        Santucci
2   Mike            Santucci
4   Matilde Josefa  Santucci
5   Milena          Santucci

In this case, you can use this query, similar to what @Popeye suggested:

SELECT id, firstname, lastname
FROM mysql_test_a tbl
WHERE (SELECT count(*) FROM mysql_test_a sbq WHERE sbq.lastname  = tbl.lastname) > 2

or this one, based on the usage of the 'in' operator

SELECT * from mysql_test_a
WHERE lastname IN (
    SELECT lastname
    FROM mysql_test_a
    GROUP BY lastname
    HAVING COUNT(lastname) >2
)

You can add 'WHERE' clauses to limit the result to 'Santucci', but I assume that a more generic answer is of interest to you.

I have also prepared a small fiddle that you can play with http://sqlfiddle.com/#!9/b1a727/16

If you are running MySQL 8.0, I would recommend a window count:

select id, firstname, lastname
from (
    select t.*, count(*) over() as cnt
    from mysql_test_a a
    where lastname = 'Santucci'
) t
where cnt > 2

We can generalize this to handle multiple last names at once:

select id, firstname, lastname
from (
    select t.*, count(*) over(partition by lastname) as cnt
    from mysql_test_a a
) t
where cnt > 2
order by lastname

The most efficient method might be exists :

select t.*
from mysql_test_a t
where lastname = 'Santucci' and
      exists (select 1
              from mysql_test_a t2 
              where t2.lastname = t.lastname and
                    t2.id <> t.id
             );

For performance, you want an index on mysql_test_a(lastname) .

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