Solved

MYSQl Query

Posted on 2014-03-27
7
249 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now