0

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.
2
  • after ON you have reviews.business_id twice. Try switching one to businesses.business_id Commented Mar 16, 2015 at 20:40
  • If your EDIT is asking a different question, make a new posting, don't add on to this one. Also, please use SHOW CREATE TABLE, it is more descriptive than DESCRIBE TABLE. Commented Mar 17, 2015 at 16:19

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

7 Comments

This didnt work for me. Can you explain it any more? I will add the user table to OP
When you say it didn't work - what happened? error? unexpected results?
ERROR 1052 (23000): Column 'user_id' in field list is ambiguous
Does that make sense? I can clarify further if needed.
I understand it now thanks. How would I find users that have reviewed every business?
|
0
SELECT u.*
FROM users u
WHERE NOT EXISTS ( SELECT 'a'
                   FROM reviews r
                   WHERE r.user_id = u.user_id
                 )

1 Comment

A little explanation would be helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.