mysql 查找具有最多低于地点平均评分的评论的评论者

cvxl0en2  于 2023-01-01  发布在  Mysql
关注(0)|答案(1)|浏览(138)

我们有两张table:
1.地点
1.评论
表详细信息示例

CREATE TABLE places (   
id INT,     
name varchar(255),  
address varchar(255),   
type varchar(255),  
average_rating INT,     
price_point varchar(255),   
total reviews INT,
PRIMARY KEY id); 

INSERT INTO places VALUES  
('1', 'Hairs to You',   '45-45', 'Queens Boulevard',    'Beauty', '4.9',    '$$$',  '36'), 
('2',  'Doggonit!',      '100',   'Atlantic Ave',      'Pet Store', '3.1',  '$$',   '52'),
('3', 'Abra Kebabra',    '193',    'Sauthoff Way',  'Restaurant',   '3.3',  '$',    '315');
CREATE TABLE reviews (  
id INT,     
user_name varchar(255),     
place_id varchar(255),  
review_date DATE,   
rating INT,     
note varchar(255),
PRIMARY KEY id,
FOREIGN KEY (place_id) REFERENCES Places(id)
);

INSERT INTO reviews VALUES 
('149', '@pinkdeb',     '8',    '2019-07-25',   '4',    'Nice little place to grab a drink'),
('117', '@ahohl',       '16',   '2019-07-29',   '3',    'The produce is always bad but otherwise okay'),
('119', '@sammyantha',  '8',    '2019-07-30',   '4',    'LOVE how kitschy this place is! Bring your visiting friends');
    • 我希望找到拥有最多低于平均评分的点评的点评者。**

你认为这是正确的密码吗?

SELECT username, name, COUNT(*)
FROM reviews
CROSS JOIN places
  ON places.id = reviews.place_id
  WHERE rating < average_rating
  GROUP BY username
  ORDER BY COUNT(*) DESC
  LIMIT 1;

我想得到帮助,以防它是错误的。

rdlzhqv9

rdlzhqv91#

考虑:

SELECT user_name, Count(ID) FROM Reviews
WHERE Rating < (SELECT Avg(Rating) AR FROM Reviews)
GROUP BY user_name
ORDER BY Count(ID) DESC LIMIT 1

你的创建表SQL错误的主键分配,所以我只是删除了测试的一部分。可能会使用一些更多的样本数据,但当我试图添加数据行,小提琴错误。
MySQL小提琴

相关问题