我需要計算列A中有重複記錄的行數,而列B必須是1並且也要被複制。MySQL查詢顯示兩列中的重複信息
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;
您的查詢在語法上不正確的。您應該提供樣本數據和期望的結果。 –
我@GordonLinoff,我盡力解釋我的問題。 – MSimoes