I have a system built on a relational MySQL database that allows people to store details of "leads". In addition, people can create their own columns under which to store data and then when adding new accounts can add data under them. The table structure looks like this:
LEADS - id, email, user_id
ATTRIBUTES - id, attr_name, user_id
ATTR_VALUES - lead_id, attr_id, value, user_id
Obviously in these tables "user_id" refers to a "Users" table that just contains people that can log into the system.
I am writing a function to output lead details and currently am just pulling through the basic lead details as a query, and then pulling through every attribute value associated with that lead (joining on the attributes table to get the name) and then joining the arrays in PHP. This is a little messy, and I was wondering if there was a way to do this in one SQL query. I have read a little about something called a "pivot table", but am struggling to understand how it works.
Any help would be greatly appreciated. Thanks!