I have 2 databases. I want to write a query that will pull data from both and tie them into the same result.
user database:
id username group
1 steve group1
2 joe group1
3 tom group2
data database:
id userid fieldname fieldresult
1 2 phone 867-5309
2 2 address 123 elm st
3 1 address 666 park avenue
If I just want steve's address, I could write:
select user.username, data.fieldresult from user, data where user.id = data.userid and data.fieldname = 'address' and user.username = 'steve';
The result would be:
username fieldresult
steve 666 park avenue
But what if I want all of the fieldresults for joe in a single row? Is that possible to do from the query itself, or do I have to handle that in code?
Basically, I'd like to see:
username phone address
joe 867-5309 123 elm st
Is this wishful thinking? I wouldn't even need "phone" and "address" as the headers, as long as I could get their values in the same result.
NOTE: I'm working with an existing database and this is how the information is currently stored.
EDIT: Also, I need to do this on a much larger basis. Instead of querying by the username, can I get this information in the same way for each user in an entire group.