所以我有以下查詢:如何使用MySQL的加入/計數正確
'SELECT *, upload.id AS id, report.id AS report_id, (SELECT COUNT(*)
FROM upload WHERE report.upload_id = upload.id) AS nums FROM upload
LEFT JOIN report ON upload.id=report.upload_id WHERE private="0" '
我有多麼每次上傳的報告正在計算的問題
。 此查詢需要計算給定上載的所有報告,並在提取時返回所有上載。由於某種原因,每次插入新報告而不是添加到總數時,我都會得到重複的上傳。見下圖:
注意如何控制檯(1)巴紐顯示了三次,每次相關於給定載報告......我真的希望它一旦出現,然後計數一邊應該是(3)。任何幫助將非常感激!!
您可以嘗試使用DISTINCT關鍵字(SELECT DISTINCT ... http://www.mysqlfaqs.net/mysql-faqs/SQL-Statements/Select-Statement/How-does-DISTINCT-work-in-MySQL ),或者您可以簡單地左連接並統計upload.id條目,並通過GROUP BY(http://www.tizag.com/mysqlTutorial/mysqlcount.php)將其分組。 – dash