make columns with left join query
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
Subject
Views
Written By
Posted
make columns with left join query
1152
July 20, 2016 03:44AM
520
July 20, 2016 03:54AM
561
July 20, 2016 12:05PM
618
July 20, 2016 12:22PM
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.