简体   繁体   English

Select 来自拥有超过 5 个标题的用户的唯一名称。 SQL

[英]Select a unique name from Users that have more than 5 titles. SQL

DB is H2(in-memory). DB是H2(内存中)。

There are two tables: Users with id, name, surname .有两个表:具有id、name、surname用户 And Documents with id, title, text, user_id .以及带有id、title、text、user_id文档

user_id is a foreign key from Users id . user_id是来自用户 id的外键。

The task is: Select a unique name from Users that have more than 5 titles.任务是: Select 来自拥有超过 5 个标题的用户的唯一名称。

I created this select, but it gives an error:我创建了这个 select,但它给出了一个错误:

SELECT DISTINCT users.name, documents.user_id,
                 ( SELECT COUNT(*)
                   FROM documents AS d
                   WHERE d.user_id = documents.user_id
                     )
                     AS rn
FROM documents, users WHERE users.id = documents.user_id
GROUP BY documents.user_id AND users.name having rn > 5 ORDER BY documents.user_id, users.name, rn;

Error: [22018][22018] Data conversion error converting "Douglas";错误: [22018][22018] 转换“道格拉斯”的数据转换错误; SQL statement: SELECT DISTINCT users.name, documents.user_id, ( SELECT DISTINCT COUNT(*) FROM documents AS d WHERE d.user_id = documents.user_id... SQL 声明:SELECT DISTINCT users.name,documents.user_id,(SELECT DISTINCT COUNT(*) FROM 文档作为用户 ID。

(Douglas is a first row name from a table) (Douglas 是表中的第一行名称)

Help me to resolve this problem, and find a mistake.帮我解决这个问题,并找出错误。

GROUP BY documents.user_id AND users.name

SQL is trying to resolve the boolean expression documents.user_id AND users.name and cannot reconcile what integer and string is supposed to be. SQL 正在尝试解析 boolean 表达式documents.user_id AND users.name并且无法协调integer and string应该是什么。

Separate multiple columns in a group by with a comma.用逗号分隔组中的多个列。

group by documents.user_id, users.name

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

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