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 > Assistance with Query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-28-11, 09:19
krisdotca krisdotca is offline
Registered User
 
Join Date: Mar 2011
Posts: 5
Assistance with Query

I'm hoping someone can help me with a query that I'm trying to construct.

I have two tables, bills and bill_items. The tables are shown below.

BILLS
-->bill_id
-->bill_date
-->customer_last

BILL_ITEMS
-->bill_id
-->item

Given the table data shown below:

=== bills ===
1, 2011-01-25, doe
2, 2011-01-27, doe

=== bill_items ===
1, A001
1, A002
1, A003
2, A001
2, B004

I would like to construct a query that produces the following output:

Inv # | Date | Last Name | Items
1 | 2011-01-25 | doe | A001 A002 A003
2 | 2011-01-27 | doe | A001 B004
Reply With Quote
  #2 (permalink)  
Old 05-28-11, 09:48
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Code:
SELECT bills.bill_id
     , bills.bill_date
     , bills.customer_last
     , GROUP_CONCAT(bill_items.item
                SEPARATOR ' ') AS items
  FROM bills
LEFT OUTER
  JOIN bill_items
    ON bill_items.bill_id = bills.bill_id
GROUP
    BY bills.bill_id
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 05-28-11, 10:24
krisdotca krisdotca is offline
Registered User
 
Join Date: Mar 2011
Posts: 5
Smile Worked Brilliantly

Worked brilliantly. Thank you so much for the response!
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