Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7575041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:35:13+00:00 2026-05-30T16:35:13+00:00

I have three tables: Table(attribute1, attribute2…); ——————————— Users(iduser, username) Link(idlink, title, userid) Comment(idcomment, content,

  • 0

I have three tables:

Table(attribute1, attribute2...);    
---------------------------------
Users(iduser, username)    
Link(idlink, title, userid)    
Comment(idcomment, content, linkid, userid)

How to select:
Link title, with corresponding username and number of comments?

I'm currently doing like this:
Q1-Select links (SELECT * FROM `links`)
Q2-Extract usernames from previous query(Q1) - (SELECT username FROM `user` WHERE iduser=Q1.userid
Q3-Extract number of comments from Q1 by id (SELECT COUNT(*) as comments FROM `comment` WHERE linkid='Q1.idlink')

I believe we can do this in much more optimized way. I got idea how to get Link with corresponding username but I got stuck when I need to count comments.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T16:35:15+00:00Added an answer on May 30, 2026 at 4:35 pm
    SELECT iduser, username, Link.title, COUNT(idcomment)
    FROM Users
    LEFT JOIN Link ON (iduser = userid)
    LEFT JOIN Comment ON (linkid = idlink)
    GROUP BY iduser, idlink
    

    Note that your Comment table is somewhat badly designed – the ‘userid’ field is not necessary, and can actually lead to situation where you’ve got a cross-linked record. e.g. a Comment belonging to user A might could be linked to a Link record belonging to user B.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this three tables. Table: Item Columns: ItemID, Title, Content, NoChange (Date) Table:
I have a table which needs to link one of three seperate tables, but
I have users table. There are three other tables: developers, managers, testers. All of
Say I have three tables: Fruit (Table 1) ------ Apple Orange Pear Banana Produce
I have three tables like this: Person table: person_id | name | dob --------------------------------
I have three tables Category, Movies and RelCatMov Category-table categoryid, categoryName 1 thriller 2
I have three tables in the many-to-many format. I.e, table A, B, and AB
I have three tables (users, articles, and tags) defined in SQLAlchemy and mapped with
I have three tables kinda like the example below: TABLE CARS carId carName -----
I have three tables/models. User, Alliance and Alliance_Membership. The latter is a join table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.