出現結果重復數SQL(四表關聯):
SELECT
COUNT(post.ID )
FROM wp_posts AS post LEFT JOIN
wp_term_relationships AS relation
ON(post.menu_order =
relation.term_order) LEFT JOIN wp_term_taxonomy
AS taxonomy
ON(relation.term_taxonomy_id = taxonomy.term_id) LEFT JOIN
wp_terms AS
term ON(taxonomy.term_id = term.term_id)
正常的結果應該顯示490條數據,但是結果顯示了224941。
解決這個辦法是在對應的COUNT()里面加上DISTINCT
DISTINCT這個關鍵字主要用於過濾掉多余的重復記錄只保留一條,但往往只用它來返回不重復記錄的條數,而不是用它來返回不重記錄的所有值。
注意:它有局限性,比如吧不能對應多個目標字段,只能對應一個目標字段。
解決重復結果書的SQL如下:
SELECT
COUNT(DISTINCT post.ID)
FROM wp_posts AS post LEFT JOIN
wp_term_relationships AS relation
ON(post.menu_order =
relation.term_order) LEFT JOIN wp_term_taxonomy
AS taxonomy
ON(relation.term_taxonomy_id = taxonomy.term_id) LEFT JOIN
wp_terms AS
term ON(taxonomy.term_id = term.term_id)
參考資料:
MySQL中count函數使用方法詳解:https://blog.csdn.net/qq_31135027/article/details/79858184