MySQL Forums
Forum List  »  Spanish

make columns with left join query
Posted by: Jonay Galván
Date: July 20, 2016 03:44AM

Well, I have two tables in mysql like this:

TABLE 1 - users
-------------
id | username | user_data
1 | George | …
2 | John | …
3 | Mary | …


TABLE 2 - services
-------------
id | id_user | service_name | service_data
1 | 1 | lunch | …
2 | 1 | dinner | …
3 | 2 | lunch | …
4 | 3 | dinner | …

Well, with this if I make a query with Left Join like this:

SELECT username, service_name
FROM users
LEFT JOIN services ON users.id = services.id_user
WHERE 1

I got this:

username | service
George | lunch
George | dinner
John | lunch
Mary | dinner

But I need to print to manage data in excel a grid like this:

username | service_1 | service_2
George | lunch | dinner
John | lunch | NULL
Mary | NULL | dinner

I have a lot of rows of users and services and it is not possible make a query asking for services for each user.

Any idea to make this with only one query or any trick in PHP?

Thanks in advance.



Regards

Options: ReplyQuote


Subject
Views
Written By
Posted
make columns with left join query
1151
July 20, 2016 03:44AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.