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 718211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:30:02+00:00 2026-05-14T05:30:02+00:00

I have a query such that I need to get A specific dog All

  • 0

I have a query such that I need to get

  1. A specific dog
  2. All comments relating to that dog
  3. The user who posted each comment
  4. All links to images of the dog
  5. the user who posted each link

I’ve tried a several things, and can’t figure out quite how to work it. Here’s what I have (condensed so you don’t have to wade through it all):

 SELECT s.dog_id,
        s.name,
        c.comment,
        c.date_added AS comment_date_added,
        u.username AS comment_username,
        u.user_id AS comment_user_id,
        l.link AS link,
        l.date_added AS link_date_added,
        u2.username AS link_username,
        u2.user_id AS link_user_id
 FROM dogs AS d
 LEFT JOIN comments AS c
 ON c.dog_id = d.dog_id
 LEFT JOIN users AS u
 ON c.user_id = u.user_id
 LEFT JOIN links AS l
 ON l.dog_id = d.dog_id
 LEFT JOIN users AS u2
 ON l.user_id = u2.user_id
 WHERE d.dog_id = '1'

It’s sorta close to working, but it’ll only return me the first comment, and the first link all as one big array with all the info i requested. The are multiple comments and links per dog, so I need it to give me all the comments and all the links. Ideally it’d return an object with dog_id, name, comments(an array of the comments), links(an array of the links) and then comments would have a bunch of comments, date_added, username, and user_id and links would have a bunch of links with link, date_added, username and user_id. It’s got to work even if there are no links or comments.

I learned the basics of mySQL somewhat recently, but this is pretty far over my head. Any help would be wonderful. Thanks!

  • 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-14T05:30:02+00:00Added an answer on May 14, 2026 at 5:30 am

    you can’t query this in 1 sql query without a lot of redundant data being sent back. because comments and links bear no relation to each other, you’d be best off doing those calls separately.

    so 3 calls in total : 1 to retrieve the dog data, 1 to retrieve comments/user, and 1 to retrieve links/user.

    SELECT d.dog_id, d.name FROM dogs AS d WHERE d.dog_id = '1';
    
    SELECT c.comment, c.date_added AS comment_date_added,
           u.username AS comment_username, u.user_id AS comment_user_id
           FROM comments AS c LEFT JOIN users AS u ON c.user_id = u.user_id
           WHERE c.dog_id = '1';
    
    SELECT l.link AS link, l.date_added AS link_date_added,
        u.username AS link_username, u.user_id AS link_user_id
           FROM links AS l LEFT JOIN users AS u ON l.user_id = u.user_id
           WHERE l.dog_id = '1';
    

    the reason for this is that although you can join the dogs/comments table well enough, when you start trying to join in the links – how do you relate a link to a comment? there’s no obvious join so your only way to do it is to use an OUTER JOIN. this will lead to a lot of redundant data in your calls.

    also, this kind of thing makes caching a lot easier and more intuitive. in my experience it’s usually easiest to go with a couple of natural queries than a spaghetti join. 🙂

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

Sidebar

Related Questions

For example I have such query: Query q = sess.createQuery(from Cat cat); List cats
I have a query on my database as such: SELECT * FROM expenses WHERE
I've noticed that a few Wordpress blogs have query statistics present in their footer
I have a query that is dynamically built after looking up a field list
I have a query that originally looks like this: select c.Id, c.Name, c.CountryCode, c.CustomerNumber,
I have a query that looks like this: public IList<Post> FetchLatestOrders(int pageIndex, int recordCount)
I have a query that I'm executing from a .NET application to a SQL
I have a query that is currently using a correlated subquery to return the
I have a query where I wish to retrieve the oldest X records. At
I have this query in sql server 2000: select pwdencrypt('AAAA') which outputs an encrypted

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.