Solved

MYSQl Query

Posted on 2014-03-27
7
255 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
  • 3
  • 2
  • 2
7 Comments
 
LVL 142

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 142

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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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 142

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

I have been using r1soft Continuous Data Protection (http://www.r1soft.com/linux-cdp/) for many years now with the mySQL Addon and wanted to share a trick I have used several times. For those of us that don't have the luxury of using all transact…
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 …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

810 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