If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > mailing label query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-26-06, 14:40
cbeshears cbeshears is offline
Registered User
 
Join Date: May 2005
Posts: 6
mailing label query

I have database of names and mailing address that I want to use to generate mailing labels. Some of the people who are in the database have the same address as others (they live in the same house) but they have their own entry. How can I generate a query that will list all the addresses without duplicating itself and show all the people who live in that one household?

For example:
John, Sue, and Mary Smith
123 Main Street
Town, ST 12345


Thank you for your help in advance.
Reply With Quote
  #2 (permalink)  
Old 06-26-06, 15:50
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
use GROUP_CONCAT on the name field(s) when using GROUP BY on all the address fields
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 06-26-06, 16:57
cbeshears cbeshears is offline
Registered User
 
Join Date: May 2005
Posts: 6
I can't quite figure out the syntax. Can you tell me what I'm doing wrong?

Code:
SELECT GROUP_CONCAT(lname, fname) FROM roster GROUP BY lname
Thanks.
Reply With Quote
  #4 (permalink)  
Old 06-26-06, 17:13
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
"list all the addresses without duplicating itself" == GROUP BY address fields

also, if you want both lastname and firstname concatenated in the GROUP CONCAT, then you need a separate CONCAT function for them
Code:
SELECT GROUP_CONCAT(CONCAT(fname,' ',lname)) as who
     , address fields
  FROM roster
GROUP
    BY address fields
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 06-26-06, 17:24
cbeshears cbeshears is offline
Registered User
 
Join Date: May 2005
Posts: 6
Perfect. Thank you very much.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On