Jump to content

combining multiple rows into single row output for spreadsheet


ultrus

Recommended Posts

Hi,

I have contact info stored in two tables. Each person in the people table has multiple pieces of contact info (phone number, email, etc.) in the info table. I can left join for multiple rows of fun (with repeating person details) like this:

SELECT p.first_name, p.last_name, i.info_type, i.info_content
FROM people p
LEFT JOIN info i ON i.person_id = p.id

 

resulting in something like this:

Bobola, Fossmosto, E, [email protected]
Bobola, Fossmosto, P, +1.111.111.1111

 

How would I alter my query to get something like this?

first_name, last_name, c1_type, c1_content, c2_type, c2_content
Bobola, Fossmosto, E, [email protected], P, +1.111.111.1111

 

Thanks for pointers in advance. :D

I got it. Never mind. It's something like:

 

SELECT

p.first_name,

p.last_name,

(SELECT i.info_type FROM info i WHERE i.person_id = p.id ORDER BY i.sortby ASC LIMIT 0,1) AS c1_type,

(SELECT i.info_type FROM info i WHERE i.person_id = p.id ORDER BY i.sortby ASC LIMIT 1,1) AS c2_type,

FROM people p

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.