MySQL - Data from two tables to one query

I've got two tables which I need to have sorted into a single query.

One of the tables contain "static" data while the other one is a rolling log of status and uptime and from there only the last result is needed for every entry in the first table.

So basicly ..

SELECT servers.id,
    servers.ipaddress,
    servers.hostname,
    servers.location,
    servers.description
FROM servers

Open in new window


and then ..

SELECT MAX(id) as entryID,
    monitor_servers.server_id,
    monitor_servers.state,
    monitor_servers.uptime
FROM monitor_servers

Open in new window


and then somehow magicly have the result come out as single rows per server.id

How's that done? I've tried all kinds of different joins but can't get it working.
MrChrisDavidsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mark BullockQA Engineer IIICommented:
SELECT servers.id,
    servers.ipaddress,
    servers.hostname,
    servers.location,
    servers.description,
    monitor_servers.id as entryID,
    monitor_servers.server_id,
    monitor_servers.state,
    monitor_servers.uptime
FROM monitor_servers, servers
WHERE servers.id=monitor_servers.server_id
and monitor_servers.id =(select max(id) from monitor_servers ms2 where ms2.server_id=servers.server_id)
0
MrChrisDavidsAuthor Commented:
:( That query also ends up with all entries from monitor_servers
0
SharathData EngineerCommented:
check this
SELECT s.id,s.ipaddress,s.hostname,s.location,s.description,
       ms2.entryID,ms1.server_id,ms1.state,ms1.uptime
  FROM servers s
  JOIN monitor_servers ms1
    ON s.id = ms1.server_id
  JOIN (SELECT server_id,max(id) as entryID
		  FROM monitor_servers
		 GROUP BY server_id) ms2
    ON ms1.server_id = ms2.server_id 
   AND ms1.id = ms2.entryID

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MrChrisDavidsAuthor Commented:
Spot on! Thanks! :D
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.