1

I need to count number of rows which have in column A duplicated records and column B must be 1 and have also to be duplicated.

SELECT COUNT(*) as num
FROM myTable
WHERE (columnB = 1 GROUP BY columnB HAVING COUNT(id_myTable) > 1) AND (columnA IN (SELECT columnA FROM myTable GROUP BY columnA HAVING COUNT(id_myTable) > 1))
ORDER BY columnC ASC`

Suppose you have these table named myTable in MySQL:

/---------------------------------/
|      id    | ColumnA | Column B |
|---------------------------------|
|          1 | Andre   |        1 |
|          2 | Joao    |        2 |
|          3 | Maria   |        1 |
|          4 | Joao    |        1 |
|          5 | Andre   |        1 |
|          6 | Maria   |        1 |
|          7 | Andre   |        2 |
/---------------------------------/

The result must be 4, because only id 1, 3, 5 and 6 has both columns duplicated and the condition for columnB must be always equal to 1.

My PHP code to give result:

$query = "select sum(c) from (select count(1) as c from myTable group by columnA, columnB having count(1) > 1 and columnB = 1) t"; $total_pages = mysql_fetch_array(mysql_query($query)); $total_pages = $total_pages[c]; $total_rows = $total_pages;

2
  • 2
    Your query is not syntactically correct. You should provide sample data and desired results. Commented Oct 21, 2017 at 15:27
  • I @GordonLinoff, I tried my best to explain my problem. Commented Oct 21, 2017 at 15:42

2 Answers 2

1
select sum(c) from (
                     select count(1) as c from myTable  
                     group by ColumnA, ColumnB 
                     having count(1) > 1 and ColumnB=1
                    ) t
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks @jophab for your reply, it worked. It gives the expected result.
I've found a problem, I've edited my question with 2 new lines in the table. In my db I've changed the status (columnB) for id 6, for example, and the result must be 4, and with your query it gives me 2.
@MSimoes I have updated my answer.` t` at end of query is an alias for the result obtained from inner query. Some alias name is a must .
thanks again. I've edited again the question because the query is in a PHP and when I use your query on PHP it gives me nothing and when I change sum(c) by * it gives me the result 2 instead of 4. Can you help me.
Finally I've see what I'm doing wrong on 3rd line of PHP. I changed the [c] for ['sum(c)'] and it worked. Thanks to jophab and @GordonLinoff
1

I believe you want:

select sum(cnt)
from (select columnA, columnB, count(*) as cnt
      from myTable
      where columnB = 1
      group by columnA, columnB
      having count(*) > 1  -- or do you mean `count(*) = 2`?
     ) ab;

2 Comments

Thanks @GordonLinoff. I mean that > 1 not equal to 2, and I need that columnB always be equal to 1, because is a status active. By the way I didn't understand ab; at the end of the query.
I've notice you have changed your reply, thanks, I think my problem is the same as with jophab's code. You can see my reply to him.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.