I have three tables:
USER: user_id (pk); username
FIELD: field_id (pk); name
METADATA: metadata_id (pk); field_id (indx); user_id (indx); value
The reasoning for this is that the application allows custom fields to be created for each user. To display all the information for a user I am building a dynamic query (via PHP) which ends up looking like this:
SELECT
u.username, m1.value AS m1value, m2.value AS m2value
FROM user AS u
LEFT JOIN metadata AS m1
ON (u.user_id=m1.user_id AND m1.field_id=1)
LEFT JOIN metadata AS m2
ON (u.user_id=m2.user_id AND m2.field_id=2)
This example has only 2 user metadata fields, but you get the idea what how this would look if there were a dozen fields.
Is there another, better way to write this query? I'm worried about the performance of this query as the users and metadata fields grow.
EDIT: I'd like to have one user per row in the returned results.