Question

I want to return all rows that have a certain value in a column and have more than 5 instances in which a number is that certain value. For example, I would like to return all rows of the condition in which if the value in the column M has the number 1 in it and there are 5 or more instances of M having the number 1 in it, then it will return all rows with that condition.

select * 
from tab 
where M = 1 
group by id --ID is the primary key of the table
having count(M) > 5;

EDIT: Here is my table:

 id |    M       | price
--------+-------------+-------

  1 |             |   100
  2 |           1 |    50
  3 |           1 |    30
  4 |           2 |    20
  5 |           2 |    10
  6 |           3 |    20
  7 |           1 |     1
  8 |           1 |     1
  9 |           1 |     1
 10 |           1 |     1
 11 |           1 |     1

Originally I just want to insert into a trigger so that if the number of M = 1's is greater than 5, then I want to create an exception. The query I asked for would be inserted into the trigger. END EDIT.

But my table is always empty. Can anyone help me out? Thanks!

Was it helpful?

Solution

Try this :

select * 
from tab 
where M in (select M from tab where M = 1 group by M having count(id) > 5);

SQL Fiddle Demo

OTHER TIPS

please try

select *,count(M) from table where M=1 group by id having count(M)>5

Since you group on your PK (which seems a futile excercise), you are counting per ID, whicg will indeed always return 1.

As i explain after this code, this query is NOT good, it is NOT the answer, and i also explain WHY. Please do not expect this query to run correctly!

select * 
from tab 
where M = 1 
group by M 
having count(*) > 5;

Like this, you group on what you are counting, which makes a lot more sense. At the same time, this will have unexpected behaviour, as you are selecting all kinds of columns that are not in the group by or in any aggregate. I know mySQL is lenient on that, but I don;t even want to know what it will produce.

Try indeed a subquery along these lines:

select * 
from tab 
where M  in 
(SELECT M
from tab 
group by M 
having count(*) > 5)

I've built a SQLFiddle demo (i used 'Test' as table name out of habit) accomplishing this (I don't have a mySQL at hand now to test it).

-- Made up a structure for testing
CREATE TABLE Test (
    id INT NOT NULL AUTO_INCREMENT, 
        PRIMARY KEY(id),
    M int
);

SELECT id, M FROM tab
WHERE M IN (
  SELECT M
  FROM Test
  WHERE M = 1
  GROUP BY M
  HAVING COUNT(M) > 5
)

The sub-query is a common "find the duplicates" kind of query, with the added condition of a specific value for the column M, also stating that there must be at least 5 dupes.

It will spit out a series of values of M which you can use to query the table against, ending with the rows you need.

You shouldn't use SELECT * , it's a bad practice in general: don't retrieve data you aren't actually using, and if you are using it then take the little time needed to type in a list of field, you'll likely see faster querying and on the other hand the code will be way more readable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top