简体   繁体   中英

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

DB is H2(in-memory).

There are two tables: Users with id, name, surname . And Documents with id, title, text, user_id .

user_id is a foreign key from Users id .

The task is: Select a unique name from Users that have more than 5 titles.

I created this select, but it gives an error:

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"; SQL statement: SELECT DISTINCT users.name, documents.user_id, ( SELECT DISTINCT COUNT(*) FROM documents AS d WHERE d.user_id = documents.user_id...

(Douglas is a first row name from a table)

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.

Separate multiple columns in a group by with a comma.

group by documents.user_id, users.name

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