1, use distinct to weight (suitable for querying the total number of the whole table)
There are multiple schools + teachers to contribute, need to count the total number of authors
Selectcount (author)As total from files each author has a lot of contributions, there are duplicate records here. Selectdistinctauthor from files; It is possible that the names of teachers in both schools are the same, and only one error is counted. SelectdistinctAuthor,sid the combined unique value of the From files statistic (author + school ID) results in the correct results, but how do you know how many people there are?
Select count (Distinct author,sid) as total from files
2, GROUP by group to go to the weight (suitable for groups based on criteria to query the total number of each group)
Select Author, count (distinct ID) from the Files group by Sid
3. Record the sum of the numbers of the two tables, and the two tables are queried separately
SELECT SUM (c)
From
(SELECT COUNT (DISTINCT from_user_id, message_id) c
From Im_message
WHERE dr = 0 and Message_status = 2 and User_type = 1 and to_user_id = 2
UNION All
SELECT COUNT (DISTINCT group_id, message_id) c
From Im_messagerefgroup
WHERE dr = 0 and user_id = 2
)
As temp;
MySQL data deduplication and record totals