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;