How to group and count in MySQL?
P粉478835592
P粉478835592 2024-04-06 18:30:17
0
1
428

I'm trying to write a query that extracts the total number of undeleted messages sent to friends. Here is a screenshot of the table structure:

The output I want to achieve is as follows:

id          messages_count
 ---------------------------
 1            4562
 2            7255

Here are the queries I have tried so far:

SELECT u.id, count( CASE WHEN m.is_deleted = false THEN m.is_deleted END ) as messages_count
FROM users u
inner join messages m on u.id = m.to_id
GROUP BY u.id
ORDER BY u.id;

P粉478835592
P粉478835592

reply all(1)
P粉662802882

Have you tried anything similar?

SELECT u.id, count( m ) as messages_count
FROM users u
inner join messages m on u.id = m.to_id and m.is_deleted = false
GROUP BY u.id
ORDER BY u.id;
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!