我需要计算列A中重复记录的行数,列B必须为1,并且还必须重复。
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`
假设您在MySQL中有这些名为myTable的表:
/---------------------------------/
| 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 |
/---------------------------------/
结果必须为4,因为只有id 1,3,5和6两列都重复,而columnB的条件必须始终等于1.
我的PHP代码给出结果:
$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;
答案 0 :(得分:1)
select sum(c) from (
select count(1) as c from myTable
group by ColumnA, ColumnB
having count(1) > 1 and ColumnB=1
) t
答案 1 :(得分:1)
我相信你想要:
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;