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 > How to write this SQL Query

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-29-11, 20:13
KingsleyHill KingsleyHill is offline
Registered User
 
Join Date: Sep 2011
Posts: 3
How to write this SQL Query

I have two tables:
Table: Names Fields: ID, Name
Table: Info Fields: ID, Key, Data

Data looks like this:
Names:
1, Joe
2, George

Info:
1, Address, 121 Main Street
1, City, Smithfield
1, State, OH
2, Address, 345 Another Street

I need a query that will show each name in Names along with any data associated with that name from Info.

Suggestions?
Reply With Quote
  #2 (permalink)  
Old 09-30-11, 03:17
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by KingsleyHill View Post
I need a query that will show each name in Names along with any data associated with that name from Info.
Code:
SELECT names.id
     , names.name
     , info.key
     , info.data
  FROM names
LEFT OUTER
  JOIN info
    ON info.id = names.id
ORDER
    BY names.name
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 09-30-11, 08:41
KingsleyHill KingsleyHill is offline
Registered User
 
Join Date: Sep 2011
Posts: 3
Get the data to a single line?

Thanks for the SQL to get the data. Now...is there a way to get one line of data for each Names record?
Reply With Quote
  #4 (permalink)  
Old 09-30-11, 09:25
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
you would do that with your application language, php or whatever
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 09-30-11, 10:58
KingsleyHill KingsleyHill is offline
Registered User
 
Join Date: Sep 2011
Posts: 3
Is there a way to do this in MySQL?

This is a reporting requirement so there is no higher level language. Is there a PL/SQL-type language available? Is there any way to do it in SQL?
Reply With Quote
  #6 (permalink)  
Old 09-30-11, 11:29
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by KingsleyHill View Post
Is there any way to do it in SQL?
sure
Code:
SELECT names.id
     , names.name
     , GROUP_CONCAT(
         CONCAT(info.key,':',info.data)
         SEPARATOR ', ' ) AS info
  FROM names
LEFT OUTER
  JOIN info
    ON info.id = names.id
GROUP
    BY names.name
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
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