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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:59:10+00:00 2026-05-15T17:59:10+00:00

These are my 2 tables: CREATE TABLE `documents` ( `Document_ID` int(10) NOT NULL auto_increment,

  • 0

These are my 2 tables:

CREATE TABLE `documents` (
  `Document_ID` int(10) NOT NULL auto_increment,
  `Document_FolderID` int(10) NOT NULL,
  `Document_Name` varchar(150) NOT NULL,
  PRIMARY KEY  (`Document_ID`),
  KEY `Document_FolderID` (`Document_FolderID`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=331 ;


CREATE TABLE `files` (
  `File_ID` int(10) NOT NULL auto_increment,
  `File_DocumentID` int(10) NOT NULL,
  `File_Name` varchar(255) NOT NULL,
  PRIMARY KEY  (`File_ID`),
  KEY `File_DocumentID` (`File_DocumentID`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=333 ;

There can be multiple files to 1 document. I am trying to SELECT all of the documents with a JOIN on the files table but I only want 1 file record which is the latest one.

Here is my query I have come up with that doesn’t quite work, can anyone suggest the right way?

SELECT `documents`.*
    FROM `documents`
      INNER JOIN (
        SELECT MAX(`File_ID`), *
        FROM `files`
        WHERE `File_DocumentID` = `documents`.`Document_ID`
        GROUP BY `File_ID` ) AS `file1`
      ON `documents`.`Document_ID` = `file1`.`File_DocumentID`
    WHERE `documents`.`Document_FolderID` = 94
    ORDER BY `documents`.`Document_Name`

*edit: the error is Unknown column ‘documents.Document_ID’ in ‘where clause’

  • 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-15T17:59:11+00:00Added an answer on May 15, 2026 at 5:59 pm

    Use:

    SELECT d.*, f.*
      FROM DOCUMENTS d
      JOIN FILES f ON f.file_document_id = d.document_id
      JOIN (SELECT t.file_document_id,
                   MAX(t.file_id) AS max_file_id
              FROM FILES t
          GROUP BY t.file_document_id) x ON x.file_document_id = f.file_document_id
                                        AND x.max_file_id = f.file_id
    

    The derived table/inline view called “x” is a join to the same table, all it does is tweak the records coming from the FILES table to be the highest per file_document_id…

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

Sidebar

Related Questions

Given these tables: create table Orders ( Id INT IDENTITY NOT NULL, primary key
Strange situation. If I have these tables: CREATE TABLE t1 (id INT, title VARCHAR(20),
There are two tables: CREATE TABLE STORE ( ID INTEGER NOT NULL, MEDICINE_ID INTEGER
There is a table: create table table1 ( id integer primary key, user_id varchar(36),
What is the difference between these to ways of table creation. CREATE TABLE TABLENAME(
i want to create a table : products which looks like (these are columns
Let's say there's a table created as follows: create table testTable ( colA int
I need to create a join query for hierarchical data across 2 tables. These
I have a simple JOINED hierarchy of documents: CREATE TABLE Documents ( id INTEGER
Looks a common situation to me: I have two tables: documents: dID (pk, int),

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.