Solved

MYSQl Query

Posted on 2014-03-27
7
261 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

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 …
Creating and Managing Databases with phpMyAdmin in cPanel.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

735 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