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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:50:47+00:00 2026-05-15T18:50:47+00:00

I need your help building a SQL statement I can’t wrap my head around.

  • 0

I need your help building a SQL statement I can’t wrap my head around.

In a database, I have four tables – files, folders, folders_files and links.

I have many files. One of them is called “myFile.txt”.

I have many folders. “myFile.txt” is in some of them. The first folder it appears in is called “firstFolder”.

I have many links to many folders. The first link to “firstFolder” is called “firstLink”.

The data structure for the example would be:

// files
Id: 10
Name: "myFile.txt"

// folders
Id: 20
Name: "firstFolder"

// folder_files (join table)
Id: 30
Folder_Id: 20 (meaning "firstFolder")
File_Id: 1 (meaning "myFile.txt")

// links
Id: 40
Name: "firstLink"
Folder_Id: 20 (meaning "firstFolder")

FIRST QUESTION: How do I get the record for “myFile.txt” AND the Name and Id of “firstLink” (the first link), querying on file Id = 10, based on the lowest Id of the folder and the link?

SECOND QUESTION: How do I get the record for “myFile.txt” AND the Name and Id of “firstLink” (the first link), querying on all files, based on the lowest Id of the folder and the link?

put another way – how do I get the first link to the first folder containing “myFile.txt”?

Resulting in a record that looks like:

Id: 10
Name: "myFile.txt"
LinkId: 40
LinkName: "firstLink"

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-15T18:50:47+00:00Added an answer on May 15, 2026 at 6:50 pm

    You should try to think about how you want your result set to look. SQL is designed to describe result sets. If you can write out a hypothetical result set, you might have an easier time writing SQL that will render that result set.

    I had a hard time understanding what you are looking for, but I’m sure it’s a fairly straight forward problem. I would be able to help you easier if you could describe you results more clearly, although you might not need my help anymore!

    For example (going with you original schema) Q1 & Q2:

    files.Id, files.Name, links.Id, links.Name (4 columns)

    Q1:

    SELECT
      files.Id, files.Name, links.Id, links.Name
    FROM
      files, links
    INNER JOIN
      folder_files
      ON files.Id = folder_files.File_Id
    INNER JOIN
      links
      ON links.Id = folder_files.Folder_Id
    WHERE
      files.Id = 10
    ORDER BY
      folder_files.File_Id ASC, links.Id ASC
    LIMIT 1;

    (JOIN with folders table not necessary)

    Q2:

    Change both ASC to DESC

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

Sidebar

Ask A Question

Stats

  • Questions 449k
  • Answers 449k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Note: These are all super old apps by now, so… May 15, 2026 at 8:08 pm
  • Editorial Team
    Editorial Team added an answer Thats easy! If you're running GNU sed, you can write… May 15, 2026 at 8:08 pm
  • Editorial Team
    Editorial Team added an answer It will be better that you drop constraint and then… May 15, 2026 at 8:08 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.