S
S
Sergey Khlopov2019-12-10 23:28:12
SQL
Sergey Khlopov, 2019-12-10 23:28:12

How to continue sampling?

Hello, tell me please, I need to select the news of those users whose number of comments > 1 As a result, I have 3 tables: comments, news, users

Structure of the users table:
-id
-login
-data
Structure of the news table:
-id
-user_id
-title
-text
The structure of the comments table:
-id
-user_id
-news_id
-text

And for example, the comments table is filled like this:
5deffdb18df49716268031.png
I make the following query:

SELECT comments.user_id
FROM comments
GROUP BY comments.user_id
HAVING COUNT(comments.user_id) > 1

I get the result:
5deffe097cb86238176679.png
Everything seems to be correct, the request returns the id of those users who left more than one comment, but according to the assignment, I still need to display their news, and please tell me how else can I find the news of these users? I understand that I need to dig towards join and subqueries?
Thank you in advance for your response.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WStanley, 2019-12-11
@Shlop

Shake!
Joints are not as scary as they seem, the main thing here is practice.

SELECT
    USERS.LOGIN
,   NEWS.TITLE
,   NEWS.TEXT
FROM
    USERS
LEFT OUTER JOIN
    COMMENTS
ON
    USERS.ID    =   COMMENTS.USER_ID
RIGHT OUTER JOIN
    NEWS
ON
    USERS.ID    =   NEWS.USER_ID
GROUP BY
    NEWS.TITLE
,   NEWS.TEXT
,   USERS.LOGIN
HAVING
    (COUNT(COMMENTS.ID) >   1)

ps To learn how to write queries, you need to write queries.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question