Solved

MYSQl Query

Posted on 2014-03-27
7
252 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
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.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

All XML, All the Time; More Fun MySQL Tidbits – Dynamically Generate XML via Stored Procedure in MySQL Extensible Markup Language (XML) and database systems, a marriage we are seeing more and more of.  So the topics of parsing and manipulating XM…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

932 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

8 Experts available now in Live!

Get 1:1 Help Now