Solved

MySQL query in Access

Posted on 2014-04-11
4
355 Views
Last Modified: 2014-04-14
A collegue requested I modify this query (for MS Access) so that he gets the latest date (adate) from the projectactivity table as well as the list of projects:

original query:
SELECT * FROM projects 
WHERE projects.active='yes'

Open in new window


I gave him this query, which works well in MySQL:
SELECT * FROM projects 
LEFT OUTER JOIN (SELECT projectactivity.projectid as id,max(adate) as latestdate FROM projectactivity WHERE projectactivity.projectid='10335-2') as derivedTable ON projects.ProjectID=id
WHERE projects.active='yes' 

Open in new window


I don't use Access enough to know why this does not work for him in Access.  

NOTE:In his MS Access setup, he is connecting to the same MySQL tables that I tested my query on.  He uses Access to format the MySQL data into printable report formats.
0
Comment
Question by:Zipbang
  • 2
4 Comments
 
LVL 54

Assisted Solution

by:Julian Hansen
Julian Hansen earned 250 total points
ID: 39993904
Try this

SELECT * FROM projects 
    LEFT OUTER JOIN (
       SELECT projectactivity.projectid as id,max(adate) as latestdate 
       FROM projectactivity WHERE projectactivity.projectid='10335-2'
     ) AS derivedTable 
     ON projects.ProjectID=derivedTable.id
WHERE projects.active='yes' 

Open in new window

0
 

Author Comment

by:Zipbang
ID: 39994366
Julian,

This helps, but I think I misunderstood his request.

He wants a query that shows  all active projects (active='yes') from the projects table and the latest adate (max(adate)) for each one when adate is in the table named projectactivity

SELECT * FROM projects 
--  join on the latest date from the projectactivity table, where projectactivity.id=projects.id
WHERE projects.active='yes'
 

Open in new window


Sorry for the confusion, am I clear here?
0
 
LVL 40

Accepted Solution

by:
Sharath earned 250 total points
ID: 39995067
remove the WHERE clause and I think you missed the GROUP BY clause.
SELECT * FROM projects 
    LEFT OUTER JOIN (
       SELECT projectactivity.projectid as id,max(adate) as latestdate 
       FROM projectactivity GROUP BY projectactivity.projectid
     ) AS derivedTable 
     ON projects.ProjectID=derivedTable.id
WHERE projects.active='yes' 

Open in new window

0
 
LVL 54

Expert Comment

by:Julian Hansen
ID: 39995911
Just checking, why do you have this filter in your sub query

WHERE projectactivity.projectid='10335-2'

Will you only ever by searching on that projectid?
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question