简体   繁体   English

涉及聚合函数的SQL查询

[英]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. 这将返回0个结果,我认为这是不对的,因为某人可以成为用户,而不是撰写评论。 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. 通过将评论表加入用户表,我们是说-让我成为所有用户,如果他们还没有留下评论,则将这些列留为NULL。 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. 然后在where子句中,我们仅选择评论列为空的那些结果,从而选择未留下评论的用户。

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
                 )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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