472,127 Members | 1,505 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,127 software developers and data experts.

Here's a Curve Ball: Limit number of records on Left Join

dlite922
1,584 Expert 1GB
I've heard this may not be possible, but never hurts to ask.

I've got three tables, Case, Violator, Alias

Case is a court police case
Violator is the person
Alias is different names (aliases) that person has.

Here's my Query:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT 
  3. c.id , 
  4. c.caseNumber , 
  5. v.lastName , 
  6. v.firstName , 
  7. a.firstName  AS aliasFirstName, 
  8. a.lastName AS aliasLastName, 
  9. v.driverLicenseNumber , 
  10. v.dateOfBirth , 
  11. t.courtDate , 
  12. c.status 
  13.  
  14. FROM 
  15. `case` as c LEFT JOIN trial as t ON c.id = t.caseID, 
  16. violator as v LEFT JOIN  alias as a ON v.id = a.violatorID 
  17.  
  18. WHERE 
  19. c.violatorID = v.id AND 
  20. ( c.status IN ('CONT','ARR','DE','FTAP','OJW','PRO','STAY','WRNT')) 
  21.  
  22. ORDER BY 1 DESC;
  23.  
  24.  
  25. //Here's some test results from fake data, some fields are cut off because of no room. 
  26.  
  27. +-----+------------+-----------+-------------+----------------+---------------+
  28. | id  | caseNumber | lastName  | firstName   | aliasFirstName | aliasLastName 
  29. +-----+------------+-----------+-------------+----------------+---------------+
  30. | 117 | A001       | DOE       | JOHN        | NULL           | NULL
  31. | 116 | 07PA36503  | AVENT     | CHRISTOPHER | NULL
  32. | 115 | 07PA33856  | APPLEBY   | KENT        | NULL
  33. | 114 | 07PA36620  | ANDERSON  | JOSHUA      | Josh
  34. | 114 | 07PA36620  | ANDERSON  | JOSHUA      | Joshi
  35. | 113 | 07PA35300  | ADNDERSON | DONALD      | NULL           | NULL
  36. | 112 | 07PA36487  | AKHTAR    | NASER       | NULL           | NULL
  37. | 111 | 07PA36488  | AKHTAR    | NASER       | NULL           | NULL
  38. | 110 | CA0003     | ARANJUEZ  | ALAN        | NULL           | NULL
  39. | 109 | CA0001     | CARINOS   | JOHNY       | Jimmy          | Car
  40. | 106 | CA0002     | MILLS     | CORRINE     | NULL           | NULL
  41. +-----+------------+-----------+-------------+----------------+---------------+
  42.  
  43. //expect result is for the second caseID 114 to not be displayed. 
  44.  
  45.  
As you can see CaseID 114 is listed twice because Joshua Anderson has 2 alias records (Josh and Joshi)

How can I change my query so that it only displays the first alias, and each case only ONCE, but i need it to search all the aliases.

That is, If i type in Joshi, it will still pull up that record, but the listing will display Josh, because that's the first alias.

Complicated I know.

Anybody up for the challenge if even possible?


Thanks,



Dan
Jul 11 '08 #1
2 1791
dlite922
1,584 Expert 1GB
Okay, I feel stupid.

Just add GROUP BY c.id !!

Tada!!

Problem Solved.


Dan
Jul 12 '08 #2
coolsti
310 100+
Just in case you are not aware of this option, if you wish to also retrieve a delineated list of all the aliases as one string variable (which you can display or manipulate after retrieving from the database), you can use the GROUP_CONCAT function when you have a GROUP BY clause.

The default deliniator in group_concat is a comma, but you can specify instead what should be used. For direct display purposes, I usually set the delineator to ', ' which is a comma followed by a space, as this looks better.
Jul 12 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Steve | last post: by
5 posts views Thread by Haisoo Shin | last post: by
2 posts views Thread by Ivor Somerset | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.