Solved

MYSQl Query

Posted on 2014-03-27
7
271 Views
Last Modified: 2014-03-28
I have a table called Reservation that has the following fields FamilyNum and OrderNum and I have a table called FamilyInfo that has the following fields FamilyNum, ParentNames, Dateadded the tables have the following data in them

Reservation Table
FamilyNum  OrderNum
1                      1
2                      3
3                      4
1                      5
1                      6

FamilyInfo Table
FamilyNum   ParentNames  DateAdded
1                       tom              2014-2-1
2                       dave             2014-2-5
1                       Tom, Mary    2014-3-1

I need a query  that will retreive all the rows in the reservation table and only the new row from the familyinfo table. So the results of the query would be:

FamilyNum OrderNum ParentNames
1                    1                Tom,Mary
2                     3               dave
3                     4               null or empty
1                     5               Tom,Mary
1                     6               Tom, Mary

Thanks
0
Comment
Question by:whiwex
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39961080
select r.FamilyNum , r.OrderNumb, fi.Name
 from Reservation  r
left join FamilyInfo fi on fi.FamilyNum = r.FamilyNum
0
 

Author Comment

by:whiwex
ID: 39961215
that will also give me  1 1 tom and 1 1 tom, mary
I don't want the 1 1 tom. I thik i need to do it with sub select
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 39961221
sorry, I overlooked that part:
select r.FamilyNum , r.OrderNumb, fi.Name
 from Reservation  r
left join FamilyInfo fi on fi.FamilyNum = r.FamilyNum 
   and not exists(select null from FamilyInfo li where li.FamilyNum = r.FamilyNum and li.DateAdded > fi.DateAdded ) 

Open in new window

0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 17

Expert Comment

by:jrm213jrm213
ID: 39961672
Actually you should just need the order by and limit statement, no need for a subselect.

Select r.FamilyNum, r.OrderNumb, fi.Name,fi.DateAdded
from Reservation r
left join FamilyInfo fi on fi.FamilyNum = r.FamilyNum
order by fi.DateAdded desc
LIMIT 1

Open in new window

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39961708
LIMIT 1 will return 1 record overall, and not 1 record per "group" ...
0
 
LVL 17

Expert Comment

by:jrm213jrm213
ID: 39962101
Sorry when I wrote that I was looking at this:
that will also give me  1 1 tom and 1 1 tom, mary
I don't want the 1 1 tom. I thik i need to do it with sub select

Select r.FamilyNum,r.OrderNumb, (Select fi.Name from FamilyInfo fi where r.FamilyNum = fi.FamilyNum order by fi.DateAdded desc LIMIT 1) as `Name` from Reservation r

Open in new window


I think either yours should work as well though
0
 

Author Closing Comment

by:whiwex
ID: 39962423
Thank You it worked great
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

636 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