简体   繁体   中英

SQL Query involving aggregate functions

I am having issues writing this query. I dont know if I should actually use count because that returns the actual count and I want to return people that havent done a review. Anyway here is my query that I am trying to write.

Find those users that haven’t reviewed any businesses.

The tables that I am using are

reviews;
+-------------+---------+------+-----+---------+-------+
| Field       | Type    | Null | Key | Default | Extra |
+-------------+---------+------+-----+---------+-------+
| business_id | int(11) | NO   | PRI | NULL    |       |
| user_id     | int(11) | NO   | PRI | NULL    |       |
| review_id   | int(11) | NO   | PRI | NULL    |       | 
| review_date | date    | YES  |     | NULL    |       |
| star_rating | int(1)  | YES  |     | 1       |     


businesses
+--------------+--------------+------+-----+---------+-------+
| Field        | Type         | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| business_id  | int(11)      | NO   | PRI | NULL    |       |
| name         | varchar(50)  | YES  |     | NULL    |       |
| city         | varchar(40)  | YES  |     | NULL    |       |
| state        | varchar(20)  | YES  |     | NULL    |       |
| full_address | varchar(120) | YES  |     | NULL    |       |

users;
+------------+-------------+------+-----+---------+-------+
| Field      | Type        | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+-------+
| user_id    | int(11)     | NO   | PRI | NULL    |       |
| name       | varchar(50) | YES  |     | NULL    |       |
| user_since | date        | YES  |     | NULL

Here is what I have so far

SELECT reviews.user_id FROM reviews
JOIN businesses ON (reviews.business_id = businesses.business_id)
GROUP BY reviews.user_id ASC
HAVING COUNT(*) > 0;

This returns 0 results and I dont think this is right because someone can be a user and not write a review. But I dont know what else I could. Thanks

EDIT: I figured out that last query but now I am trying to complete this one!

Find the users that have reviewed every business.

You want the users table not the business table.

By left joining the reviews table to the users table we are saying - get me all users and if they haven't left a review just leave those columns as NULLs. then in the where clause, we are selecting only those results where the review columns are nulls, thereby selecting users who haven't left a review.

select u.User_ID 
from 
    users u
    left join reviews r
        on r.user_id = u.user_id
where r.review_id is null
SELECT u.*
FROM users u
WHERE NOT EXISTS ( SELECT 'a'
                   FROM reviews r
                   WHERE r.user_id = u.user_id
                 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM